-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPage.xaml.cs
59 lines (47 loc) · 1.77 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace FindMe;
public partial class MainPage : ContentPage
{
// Bing provides a URL format that supports opening a location
// with latitude and longitude coordinates.
string baseUrl = "https://bing.com/maps/default.aspx?cp=";
public string UserName { get; set; }
public MainPage()
{
InitializeComponent();
}
private async void OnFindMeClicked(object sender, EventArgs e)
{
var permissions = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (permissions == PermissionStatus.Granted)
{
await ShareLocation();
}
else
{
await App.Current.MainPage.DisplayAlert("Permissions Error", "You have not granted the app permission to access your location.", "OK");
var requested = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
if (requested == PermissionStatus.Granted)
{
await ShareLocation();
}
else
{
await App.Current.MainPage.DisplayAlert("Location Required", "Location is required to share it. We'll ask again next time.", "OK");
}
}
}
// Gets the user’s name and shares their location.
private async Task ShareLocation()
{
UserName = UsernameEntry.Text;
var locationRequest = new GeolocationRequest(GeolocationAccuracy.Best);
var location = await Geolocation.GetLocationAsync(locationRequest);
await Share.RequestAsync(new ShareTextRequest
{
Subject = "Find me!",
Title = "Find me!",
Text = $"{UserName} is sharing their location with you",
Uri = $"{baseUrl}{location.Latitude}~{location.Longitude}"
});
}
}