'C# UWP Geolocation, access denied

everybody.

I am trying to get current user's location in C# UWP application.

var geoLocator = new Geolocator();
geoLocator.DesiredAccuracy = PositionAccuracy.High;
var accessStatus = await Geolocator.RequestAccessAsync();
var pos = await geoLocator.GetGeopositionAsync();
var latitude = pos.Coordinate.Point.Position.Latitude;
var longitude = pos.Coordinate.Point.Position.Longitude;

But I get the error:

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code WinRT information: 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. Additional information: Access is denied.

In Package.appxmanifest I have these items checked: Internet (Client & Server) - checked, Internet (Client) - checked, Location - checked.

How can I set permission in local machine?



Solution 1:[1]

I solved this issue. In Windows 10, in Location privacy settings my Location services wasn't on. So I turned it on, and now it is working.

Solution 2:[2]

You need to check either you are allowed to access location services or not ? You can handle this using RequestAccessAsync() method of GeoLocater Class in UWP.

        Geolocator geoLocator = new Geolocator();
        GeolocationAccessStatus accessStatus = await Geolocator.RequestAccessAsync();

        if ( accessStatus == GeolocationAccessStatus.Allowed)
        {
            // Put all your Code here to access location services
            Geoposition geoposition = await geoLocator.GetGeopositionAsync();
            var position = geoposition.Coordinate.Point.Position;
            var latlong = string.Format("lat:{0}, long:{1}", position.Latitude, position.Longitude);
        }
        else if (accessStatus == GeolocationAccessStatus.Denied)
        {
               // No Accesss
        }
        else
        {
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3239349
Solution 2 Ammar