Reputation: 535
I am trying to get the geolocation of my device like this:
Geolocator geo = new Geolocator();
double lat = 0, longt = 0;
Task getPosition = Task.Run(async () =>
{
try
{
Geoposition pos = await geo.GetGeopositionAsync();
lat = pos.Coordinate.Point.Position.Latitude;
longt = pos.Coordinate.Point.Position.Longitude;
}
catch(Exception exp)
{
Debug.WriteLine(exp);
}
});
getPosition.Wait();
But, I'm getting the following exception:
Your App does not have permission to access location data. Make sure you have defined ID_CAP_LOCATION in the application manifest and that on your phone, you have turned on location by checking Settings > Location.
Any idea of what could be the problem? Thank you!
Upvotes: 0
Views: 2491
Reputation: 30152
You mentioned laptop, but the error message is for a phone. Are you running this in the phone's emulator and have you turned it on in your phone? IF not just turn it on in Settings-> Location. If not, send me the project adamt at microsoft and I'll duplicate and fix and post update here or try to duplicate on my end.
Edit - I've seen the project Solution: You need to include Geolocator.RequestAccessAsync() from Windows 10 onwards. This will prompt the user to allow the location request. IN your specific example ensure you mark you button1_click with async:
private async void Button_Click(object sender, RoutedEventArgs e)
From https://msdn.microsoft.com/en-us/library/windows/apps/br225537.aspx
var accessStatus = await Geolocator.RequestAccessAsync(); switch (accessStatus) { case GeolocationAccessStatus.Allowed: _rootPage.NotifyUser("Waiting for update...", NotifyType.StatusMessage); // If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used. Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue }; // Subscribe to StatusChanged event to get updates of location status changes _geolocator.StatusChanged += OnStatusChanged; // Carry out the operation Geoposition pos = await geolocator.GetGeopositionAsync(); UpdateLocationData(pos); _rootPage.NotifyUser("Location updated.", NotifyType.StatusMessage); break; case GeolocationAccessStatus.Denied: _rootPage.NotifyUser("Access to location is denied.", NotifyType.ErrorMessage); LocationDisabledMessage.Visibility = Visibility.Visible; UpdateLocationData(null); break; case GeolocationAccessStatus.Unspecified: _rootPage.NotifyUser("Unspecified error.", NotifyType.ErrorMessage); UpdateLocationData(null); break; }
Upvotes: 2