Forum Xamarin.Forms

Can't get location


I'm using Xamarin.Mobile to get the curernt location for the device. I have read the it does not work in the simulator.
Now I have testet it on a device (IOS 9.1)

But with no luck.
The GetPositionAsync never returns.

`public async Task GetCurrentLocation()
var locator = new Geolocator { DesiredAccuracy = 50 };
var cancelSource = new CancellationTokenSource();
var location = GeoLocation.Undefined;

        await locator.GetPositionAsync(1000, cancelSource.Token).ContinueWith(
            t => location = t.IsCompleted 
                ? new GeoLocation(t.Result.Latitude, t.Result.Longitude) 
                : GeoLocation.Undefined, 

        return location;

Anyone that knows what to do?

Br Morten


Best Answers


  • Hello all!

    Similar problem here

  • Yes, I have already tried this. But with no luck.

    var position = await locator.GetPositionAsync(10000);

    never returns

  • Antonio.2859Antonio.2859 USMember

    the same problem, how do you solve it Morten?
    when i call from my pcl project var position = await locator.GetPositionAsync(10000); it never returns...

  • RenaudLaloireRenaudLaloire BEUniversity ✭✭

    Hey, same problem here.

    I'm sending a silent remote notification to wake up the device, get his location using locator.GetPositionAsync but this method never returns.

    All the config like UIBackgroundModes key, allowsBackgroundLocationUpdates and NSLocationAlwaysUsageDescription is OK.

    Anyone on this?

    Thank you

  • DamienRamseyDamienRamsey IEUniversity ✭✭
    edited May 2016

    Has anyone ever got a solution for this ?
    Im in android and pushed it out to a class... but never returns :neutral:
    No errors etc....

    public async Task GetLocationAsync() ..... var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 1000; if (locator.IsGeolocationEnabled){ position = await locator.GetPositionAsync(20000); }

    called from separate class
    using(DeviceLocation deviceLocation = new DeviceLocation(globalContext)) { await deviceLocation.GetLocationAsync(); current_Lat = deviceLocation.current_Lat(); current_Lng = deviceLocation.current_Lng(); }

    Now the funny thing is :sweat_smile:
    If i put code on my main cs as per example code on git (link above in other comment) it works no problems....

    Its as if its not awaiting or something else is going on ..

  • AtishooMazumderAtishooMazumder USMember ✭✭

    Using GeoLocator Plugin Version 3.0.4 (Stable) and 4.0.* Beta targeting Android 7.1.
    Its unable to fetch location using Stable (3.0.4) Plugin Version. But with beta version of Plugin its giving "Could not resolve type with token 010001fe" xamarin.geolocator. But Plugin is working fine for Lower target versions.

  • kmalkmal ARMember

    try this var position = await locator.GetPositionAsync(timeout: new TimeSpan(10000));

  • NiteshLuharukaNiteshLuharuka USDeveloper Group Leader ✭✭
    edited November 2017

    This works like a charm - var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10000), null, true);

  • BFaruqiBFaruqi USMember

    Thanks Nitesh
    For the latest stable verison 4.1.3 this work like you said.
    var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10000), null, true);

  • LarryKenobiLarryKenobi USMember ✭✭

    I had this working then tried to make some unit tests to test my class that uses Geolocator. It is in an android test runner with the proper nuget packages and this just refuses to work. It didn't work originally in my project but something fixed it.

    Why can't I just get the position?

  • In case no-one can get it fixed with the fix above please try to confirm that the location mode on the android device is set to high Accuracy.

    I had it in "Device Only" and the GetLocationAsynch was always timing out.

    On Lenovo Android device go to Settings -> Security&Location -> Location -> Mode and set it to "High Accuracy"

  • You could implement your servince in ios and get last position from it.

    [assembly: Dependency(typeof(LocationService))]
    namespace YourApp.iOS.Services
        public class LocationService : INativeLocation
            private readonly CLLocationManager _locMgr;
            public LocationService()
                this._locMgr = new CLLocationManager();
                this._locMgr.PausesLocationUpdatesAutomatically = false;
                // iOS 8 has additional permissions requirements
                if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                    _locMgr.RequestAlwaysAuthorization(); // works in background
                    //locMgr.RequestWhenInUseAuthorization (); // only in foreground
                if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
                    _locMgr.AllowsBackgroundLocationUpdates = true;
            public Position GetLastKnonwPossition()
                var location = _locMgr.Location;
                return new Position(location.Coordinate.Latitude, location.Coordinate.Longitude);
            public void Start()
                if (CLLocationManager.LocationServicesEnabled)
                    //set the desired accuracy, in meters
                    _locMgr.DesiredAccuracy = 1;
                    _locMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs args) =>
                        if (!args.Locations.Any()) return;
                        var position = args.Locations[0].Coordinate;
                        var accuracy = args.Locations[0].VerticalAccuracy;
                        // fire our custom Location Updated event
Sign In or Register to comment.