0

I am trying to get current location for WinRT application. But PositionChanged event never gets fired. Any help is highly appreciated !

In Package.appxmanifest, I have enabled Location. My code is below:-

    Geolocator geolocator = new Geolocator { ReportInterval = 2000 };
    geolocator.DesiredAccuracyInMeters = 100;  // specify your range
    geolocator.PositionChanged += OnPositionChanged;

private async void OnPositionChanged(Geolocator sender, PositionChangedEventArgs e)
        {
            if (this.previousLocationState == LocationStates.PopupEmpty && (viewModel.Coordinate == null || viewModel.Coordinate.Latitude != prevGpsCoordinate.Latitude || viewModel.Coordinate.Longitude != prevGpsCoordinate.Longitude))
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    this.viewModel.Coordinate = new GpsCoordinate { Latitude = e.Position.Coordinate.Point.Position.Latitude, Longitude = e.Position.Coordinate.Point.Position.Longitude };
                    Field.GpsCoordinate gpsCoord = new Field.GpsCoordinate { Latitude = viewModel.Coordinate.Latitude, Longitude = viewModel.Coordinate.Longitude };
                    CreateMapPin(gpsCoord);
                    prevGpsCoordinate = this.viewModel.Coordinate;
                });
            }
        }
4
  • Have you called the static method RequestAccessAsync beforehand? Commented Jul 17, 2018 at 10:21
  • Hi @SteveJ There is no method called RequestAccessAsync available in Geolocator class Commented Jul 17, 2018 at 10:40
  • It's static. You don't call it on the instance. Commented Jul 17, 2018 at 10:41
  • There is no such static method available... Only Equals and ReferenceEquals static methods are. Commented Jul 17, 2018 at 10:49

2 Answers 2

0

I think you need to do this:

await Geolocator.RequestAccessAsync();
Geolocator geolocator = new Geolocator { ReportInterval = 2000 };
geolocator.DesiredAccuracyInMeters = 100;  // specify your range
geolocator.PositionChanged += OnPositionChanged;
await geolocator.GetGeopositionAsync();

Putting this as an answer because I can't post code in a comment.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using Windows 8.1 and I have set Location privacy setting as on. var geoposition = await geolocator.GetGeopositionAsync(); This checks if i location is enabled. But getting exception as "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."
Can you add the section of the manifest where you enable location to the question?
0

I got it working by putting this code in Package.appxmanifest

<DeviceCapability Name="location" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.