Cross Platform Location

MartHughMartHugh USMember ✭✭✭

I would like to use Xamarin Forms to target at least two platforms. One item we will need to collect from the device is its location. I have been looking at the docs and this seems to require platform specific methods to recover this information.

Have I missed something, is this not available in a platform independent API?



  • MartHughMartHugh USMember ✭✭✭

    As kindly suggested in another thread, I will look at Xamarin.Forms.Labs. Any other suggestions welcomed. Thanks

  • RyanDavisRyanDavis AUInsider, University ✭✭

    @JamesMontemagno's Geolocator plugin should sort you out!

    The list here is a good catalogue of community developed PCL-friendly abstractions.

  • MartHughMartHugh USMember ✭✭✭

    I have added this and using the sample code which comes with the plugin and it works OK on Android.

    I can call the sample code repeatedly by invoking it from a button and the timestamp information (which is displayed) updates on each invocation.

    However, IOS is another matter. I have tried to follow the "Important" instructions for IOS at
    and have added the NSLocationWhenInUseUsage string.

    But, I have no idea how to make the initialisation call to
    from Xamarin.Forms. It does not appear to be part of the plugin object model either.

    When I run the IOS code without this initialisation, it appears to work once (I get the Allow Location Dialogue displayed) and the timestamp and lat long are displayed. But any subsequent invocations do not update any of the position fields, in particular the timestamp field is never updated.

    Any ideas what I am doing wrong? How should I call the RequestWhenInUseAuthorization API, and where should I call it from.

    Thanks !

  • SKallSKall USMember ✭✭✭✭

    XLabs.Platform has geolocator implementation as well (available from NuGet):

    You will need to do the iOS permissions on the iOS specific project. More precisely in the Info.plist file:

    Note: on IOS you need to add NSLocationWhenInUseUsageDescription key to your Info.plist file with a string value for the message to show to the user to request access.

  • MartHughMartHugh USMember ✭✭✭

    I have tried to use this but unfortunately after changing the IOS class AppDelegate to
    public partial class AppDelegate : XFormsApplicationDelegate

    I get this error
    The type or namespace XFormsApplicationDelegate could not be found are you missing an assembly reference.

    I am using a PCL and the PCL, Android and IOS projects all reference the Xamarin.Forms.Labs assembly.
    I have also added
    using Xamarin.Forms.Labs;
    To the AppDelegate.cs file

    Any ideas what I am missing ?

  • SKallSKall USMember ✭✭✭✭

    @MartHugh is the iOS application targeting Xamarin.iOS? If not then migrate to Xamarin.iOS and reinstall XLabs.Forms from NuGet.

  • MartHughMartHugh USMember ✭✭✭

    @SKall In order to use Xamarin.Forms.Labs package, should I be referencing XLabs or Xamarin Forms Labs ? What is the difference. I see the two terms used together in documentation, but I was surprised to see two distinct packages.

  • MartHughMartHugh USMember ✭✭✭
    edited May 2015

    @TyKramer Many thanks for this source code. For this package I was indeed missing the callback. I will give it a try.

    Edit: I notice there is no call to RequestWhenInUseAuthorization, so I assume this will not work on IOS 8.3 ?

  • TyKramerTyKramer USMember ✭✭

    @MartHugh On first run in the iOS 8.3 simulator, I'm prompted to allow location access. After I allow, this message is logged: "Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first." But GPS and the Geolocator plugin work fine regardless and subsequent runs don't prompt or log a message.

    So, there's probably something else that can/should be set for iOS. But I'm just amazed it all worked on Android/iOS simulators and an Android device, with no platform specific code. Good introduction to the platform for me!

  • MartHughMartHugh USMember ✭✭✭

    For me it only works first time on the IOS 8 emulators.
    The reason I know this is that it does not matter if I use the position changed event or call GetPositionAsync from a timer, under IOS the locator position timestamp returned never updates from the time it is on the very first call.

    This is the routine I call from a timer, Timestamp never changes on IOS 8, but does on Android.
    // ================================================
    private async void setgeo()
    var position = await locator.GetPositionAsync(timeout: 10000);
    thetext.Text="Status:" + position.Timestamp + " Lat:" + position.Latitude + " Long:" + position.Longitude;


    What I cant tell, is whether the timestamp is only updated when the lat long updates. Since I am running an emulator on a Mac the lat long is not expected to change.

    Is there anyway I can upload this to an Apple Device to check this out ?

  • SKallSKall USMember ✭✭✭✭

    @MartHugh it would be XLabs

  • MartHughMartHugh USMember ✭✭✭

    @ Skall. Thanks. Browsing Nuget can be a little confusing, there seems to be all sorts of mix and match packages with the same or similar description. Will use XLabs. Cheers.

  • TyKramerTyKramer USMember ✭✭

    @MartHugh You can get a continually changing lat/long on the iOS simulator by selecting Debug > Location > Freeway Drive

  • JoaoFortesJoaoFortes ESMember ✭✭

    Anybody knows if is possible to get gps time every second?

    I tried with Geolocator PositionChanged and GetPositionAsync but no success in IOS.

    On Android works fine.

  • If not want to use a plugin
    then use these lines of code in IOS in dependency from pcl


    you will get (0,0) as coordinates when use in emulator, but works well in a device try these

    CLLocationManager locationManager = new CLLocationManager();
    public void GetPresentLocation()
                locationManager.RequestAlwaysAuthorization(); //to access user's location in the background
                locationManager.RequestWhenInUseAuthorization(); //to access user's location when the app is in use.
                    locationManager.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) =>
                        //you will get coordinates when ever your location gets updated
                    if (CLLocationManager.LocationServicesEnabled)
                        Console.WriteLine("Location services not enabled, please enable this in your Settings");
                    var location = locationManager.Location.Coordinate;
                catch (Exception ex)
                    var msg = ex.Message;
  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭

    Hi, I done this in Xamarin.Forms, By using Dependency. here is the sample.......

  • alexmartinezalexmartinez USMember ✭✭

    @Vaka.GopiNadhReddy said:
    Hi, I done this in Xamarin.Forms, By using Dependency. here is the sample.......

    I have downloaded your sample but cannot be implemented in my device despite it compiles successfully.

  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭


    i think the problem is due to gps. you have to enable gps in your device.

  • alexmartinezalexmartinez USMember ✭✭

    @Vaka.GopiNadhReddy That's it! I am using Marshmallow (API 23) and the app is not requesting the permissions in execution time. I will check it out how to implement it.

    Your code snipet is way powerful than Geolocator.

  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭

    Thank you

  • Praveen.BPraveen.B INMember ✭✭

    Hi every one does the GeoLocator plugin works in sleep mode of the app and mobile lock stage.

    please provide me a solution for my problem in this below link

Sign In or Register to comment.