Search code examples
c#androidgoogle-mapsxamarin.formsgeolocation

Xamarin Forms Geolocator Continuously Updating the Location


I'm looking to continuously update the device's current location on the Google Map.

I have to implement my own blue dot for various reasons. What I have done is:

  • I hide the Google Map's default My Location marker (default blue dot).

  • I draw my own blue dot

  • I created a thread that keeps calling the Geolocator to update its current position: await locator.GetPositionAsync(TimeSpan.FromSeconds(2), null, false);

However, the position update takes a significant delay. I am looking to update the position at a very low interval (around 50 ms) which could give me a similar result to the Google Map's original blue dot when the device is moving.

How can I achieve this?


Solution

  • I found out that CrossGeolocator has a listening function which will prompt if there is a position update. Rather than keep calling the current position, this function will continuously listen to any position changes.

    if (CrossGeolocator.IsSupported &&
        CrossGeolocator.Current.IsGeolocationAvailable &&
        CrossGeolocator.Current.IsGeolocationEnabled)
    {
        CrossGeolocator.Current.PositionChanged += HandlePositionChanged;
        await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 10);
    }