Ganesh Parikh
Ganesh Parikh

Reputation: 244

How to get GPS location instantly in Xamarin.forms?

I am capturing the Latitude & Longitude on Listview_ItemTapped by using the Geolocator plugin to get GPS Location in xamarin.forms. But takes average more than 15 second to capture location. Is there any way to get location instantly.

if (CrossGeolocator.Current.IsGeolocationEnabled == false)
 {
    await DisplayAlert("Notification", "Geolocation is Unavailable ", "Ok");
 }
 else
 {
    Application.Current.Properties["LATITUDE"] = null;
     Application.Current.Properties["LONGITUDE"] = null;

    var locator = CrossGeolocator.Current;
    //locator.DesiredAccuracy = 50;               
    var position = await locator.GetPositionAsync(timeoutMilliseconds: 50000);

    Application.Current.Properties["LATITUDE"] = position.Latitude;
    Application.Current.Properties["LONGITUDE"] = position.Longitude;

    await Navigation.PushAsync(new RetailerDetailsPage(), false);
 }

I tried by removing locator.DesiredAccuracy = 50; but again sometime it works instantly & sometime its not.

Upvotes: 4

Views: 6222

Answers (1)

SushiHangover
SushiHangover

Reputation: 74209

Yes you can get a quicker response, but it depends on your requirements.

The long answer involves understanding that each device OS & model, the accuracy requested, the GPS acquisition time, your current physical environment (indoors, outdoor, physical shading of GPS satellites, ...), etc... all effect how long it takes to get a full GPS response back.

While the Xamarin.Plugin Geolocator is great and I use it all the time, it does not provide access to all the various platform-specific location features.

On iOS you could use the RequestLocation on CoreLocation which Apple describes as "a quick fix" on the user's location. This will perform the fastest GPS power up, acquire and power down that is available. It is great for getting that quick one-shot location.

On Android you could use Google's Play FusedLocationProviderApi (FusedLocationApi) and grab the users location via GetLastLocation. This will return the location immediately (with a small chance that it might be null).

Using Google location service, you can get a location based upon just the user's wifi connection (SSID-based), etc...

So via using a Forms' platform-specific Dependency Service, you can obtain a quick location and then use Xamarin.Plugin's Geolocator to obtain a more accurate and/or more recent location.

This is exactly what we do on an background thread in an app at startup startup time as the first view that the user sees is their location on a custom map, than when an a "full" GPS location is obtained from the background thread (and possibly more accurate location) we can adjust the map...

Upvotes: 5

Related Questions