简体   繁体   中英

Subscribe to xamarin map/Geolocator event when the user's current location changes

I'm creating a xamarin app using xamarin.forms.maps and wanted to know if there is a way to subscribe to an event that tells me when the user's current location changes. I can get the user's current location via the geolocator plugin but I want an event-driven way to update the current location when the user moves since I want to save the user's current location to a DB when they move x meters. I've searched high and low and was not able to find what I'm really looking for. Any help would be appreciated.

There are plenty of samples available in the official documentation.

As for instance, you can subscribe to location updates in Xamarin.Android by subscribing to FusedLocationProviderClient.RequestLocationUpdatesAsync as read here . You can also use the LocationService , but as mentioned in the documentation , this service is both an older API and is best used for devices which do not have Google Play Services installed.

You can find a sample of using the FusedLocationProviderClient here and another sample using the LocationService here .

For iOS, you can subscribe to the CLLocationManager.LocationsUpdated event as described here . You can find a sample of this here .

You can then, as for instance, create your own, ILocationManager which is implemented platform specifically to listen for location updates in your PCL project.

To anyone who might come across this,

Geolocator has an event called PositionChanged that can be subscribed to in the PCL. Sadly enough it was easy to find when I was looking at the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM