Geolocation

PaulPPaulP USMember ✭✭

What is the best way to get geolocation?

Is there an existing PCL that can be used?
Do you write custom code for each platform?
Xamarin.Mobile component?
Mvvmcross location plugin (how can I get this working with Xamarin.Forms)?

Thanks all,
Paul

Posts

  • JasonAwbreyJasonAwbrey USInsider, University, Developer Group Leader mod

    Xamarin.Forms.Maps has a built in cross-platform geolocation API. Otherwise I would try Xamarin.Mobile

  • PaulPPaulP USMember ✭✭

    I am looking for a way to retrieve the current location (lat & lon). Doesn't look like that's supported by the Maps module. Or am I missing something?

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    @PaulP You can still get the current location using Xamarin.Mobile (https://components.xamarin.com/view/xamarin.mobile)

    To get the user's location (requires ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions on Android):

    using Xamarin.Geolocation;
    // ...
    
    var locator = new Geolocator { DesiredAccuracy = 50 };
    //            new Geolocator (this) { ... }; on Android
    
    Position position = await locator.GetPositionAsync (timeout: 10000);
    
    Console.WriteLine ("Position Status: {0}", position.Timestamp);
    Console.WriteLine ("Position Latitude: {0}", position.Latitude);
    Console.WriteLine ("Position Longitude: {0}", position.Longitude);
    
  • PaulPPaulP USMember ✭✭

    @JonDouglas‌ would you use that code within each of your UI projects or is there a way to use Xamarin.Mobile within a PCL(Core project)?

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai
    edited June 2014

    You would probably use that code within each of your UI projects as I don't believe it's PCL-compat(And it has different versions for iOS/Android). However you could always create an Interface at your PCL level, and implementations on the platform level. You would then use DI/IoC to resolve the proper implementation when writing core logic.

  • SKallSKall USMember ✭✭✭✭
  • PaulPatarinskiPaulPatarinski USUniversity ✭✭
    edited June 2014

    @Skall how should I go about implementing?

    Pull down https://github.com/XForms/XForms-Toolkit
    Compile
    Reference XForms.Toolkit.Droid in my Droid UI project
    Reference XForms.Toolkit in my Core (pcl project)
    DependencyService.Get<IGeolocator>();
    

    Does that look right to you?

  • SKallSKall USMember ✭✭✭✭

    Yes, that looks correct.

Sign In or Register to comment.