Forum Xamarin Xamarin.Forms

Getting current location in Xamarin.Forms for Android

Terence.6876Terence.6876 USMember ✭✭


What I want to do is quite simple; provide a button that when the user clicks, connects to the Location service and retrieve the Longitude and Latitude of the current location. I've spent hours trying to achieve this using the sample code for Location Service, but it does not work. I don't have any errors, but the OnLocationChanged() does not get fired at all. I am not trying to track location every time it changes, all I want to do is pickup the current location when needed - very simple requirement. Can anyone help please?




  • JohnHardmanJohnHardman GBUniversity admin
    edited July 2017

    @Terence.6876 - Have you checked that your app has the necessary permissions? Does the device you are running your app on receive GPS location where you are using it? Are you using sensible values for DesiredAccuracy and TimeoutInMilliseconds? (if you've ever been stuck on a broken down boat with a GPS and a radio, you may know how long it can take to get your position before you can radio for help).

    The basics are as follows, but there are ways of improving on this once the basics are working.

            private Plugin.Geolocator.Abstractions.IGeolocator _locator;
                    _locator = CrossGeolocator.Current;
                    _locator.DesiredAccuracy = DesiredAccuracy;
                    _locator.AllowsBackgroundUpdates = true;
                        var position = await _locator.GetPositionAsync(TimeoutInMilliseconds);
                        if (position != null)
                            _currentLatitude = position.Latitude;
                            _currentLongitude = position.Longitude;
  • RavinderJangraRavinderJangra USMember ✭✭✭
Sign In or Register to comment.