async Task StartLocationUpdatesAsync() { // Create a callback that will get the location updates if (locationCallback == null) { locationCallback = new MyLocationCallback(); locationCallback.LocationUpdated += OnLocationResult; } // Get the current client if (client == null) { client = LocationServices.GetFusedLocationProviderClient(this); } try { //Create request and set intervals: //Interval: Desired interval for active location updates, it is inexact and you may not receive upates at all if no location servers are available //Fastest: Interval is exact and app will never receive updates faster than this value var locationRequest = new LocationRequest() .SetInterval(10000) .SetFastestInterval(5000) .SetPriority(LocationRequest.PriorityHighAccuracy); await client.RequestLocationUpdatesAsync(locationRequest, locationCallback); } catch (Exception ex) { //Handle exception here if failed to register } }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.main); var toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); if (toolbar != null) { SetSupportActionBar(toolbar); SupportActionBar.SetDisplayHomeAsUpEnabled(false); SupportActionBar.SetHomeButtonEnabled(false); } startButton = FindViewById <Button>(Resource.Id.start_updates_button); startButton.Click += async(sender, args) => await StartLocationUpdatesAsync(); stopButton = FindViewById <Button>(Resource.Id.stop_updates_button); stopButton.Click += (sender, args) => StopLocationUpdates(); latitudeTextView = FindViewById <TextView>(Resource.Id.latitude_text); longitudeTextView = FindViewById <TextView>(Resource.Id.longitude_text); timeTextView = FindViewById <TextView>(Resource.Id.last_update_time_text); fusedLocationClient = LocationServices.GetFusedLocationProviderClient(this); settingsClient = LocationServices.GetSettingsClient(this); if (bundle != null) { var keys = bundle.KeySet(); if (keys.Contains(KeyRequesting)) { requestingUpdates = bundle.GetBoolean(KeyRequesting); } if (keys.Contains(KeyLocation)) { currentLocation = bundle.GetParcelable(KeyLocation) as Location; } if (keys.Contains(KeyLastUpdated)) { lastUpdateTime = bundle.GetString(KeyLastUpdated); } } locationCallback = new MyLocationCallback(); //Create request and set intervals: //Interval: Desired interval for active location updates, it is inexact and you may not receive upates at all if no location servers are available //Fastest: Interval is exact and app will never receive updates faster than this value locationRequest = new LocationRequest() .SetInterval(UpdateInterval) .SetFastestInterval(FastestUpdateInterval) .SetPriority(LocationRequest.PriorityHighAccuracy); locationSettingsRequest = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest).Build(); UpdateUI(); }