private static GoogleGeocoding.Results TryGetAddress(string address, ref bool hasLatLng) { try { var results = GeoServices.GeocodeAddress(address); hasLatLng = results != null && results.GetLocation().lat != 0 && results.GetLocation().lng != 0; return(results); } catch { hasLatLng = false; return(null); } }
public override bool OnStartJob(JobParameters @params) { if (_isStarted) { return(false); } App.EndTime = DateTime.Now; TimeSpan diffTime = App.EndTime - App.StartTime; Android.Content.Context context = Application.Context; Task.Run(() => { try { // Log.Debug(TAG, $"Inicia Servicio GeoLocation ejecutado a las {startTime}, flags = {flags}, startid = {startId}"); Log.Debug(TAG, $"Inicia Servicio GeoLocation ejecutado a las {startTime}"); if (_isStarted) { TimeSpan runtime = DateTime.UtcNow.Subtract(startTime); Log.Debug(TAG, $"El Servicio GeoLocation ya esta iniciado, Este se ha ejecutado a las {runtime:c}."); } else { _locationManager = (LocationManager)GetSystemService(LocationService); startTime = DateTime.UtcNow; Log.Debug(TAG, $"Inicio del Servicio de GeoLocation, a las {startTime}."); timer = new Timer(async delegate(object state) { var request = new GeolocationRequest(GeolocationAccuracy.Best); //Xamarin.Essentials.Location location = await Geolocation.GetLastKnownLocationAsync(); //if (location == null) //{ // string Address = string.Empty; // var locations = await Geocoding.GetLocationsAsync(Address); // location = locations?.FirstOrDefault(); //} //var request = new GeolocationRequest((GeolocationAccuracy)accuracy); CancellationToken cts = new CancellationToken(); try { var location = await Geolocation.GetLocationAsync(request, cts).ConfigureAwait(false); if (location != null) { GeoServices _geoServices = new GeoServices(location); await _geoServices.UpdateGeolocation().ConfigureAwait(false); Log.Debug(TAG, $"Actualización en BBDD, a las {startTime}."); } else { Log.Debug(TAG, $"No se pudo obtener la geolocalización a las {startTime}."); } } catch (Exception fail) { Log.Debug(TAG, $"Excepcion: {fail.Message}"); } }, startTime, 0, TimerWait); _isStarted = true; } JobFinished(@params, true); return(_isStarted); // StartCommandResult.NotSticky; } catch (Exception fail) { Log.Debug(TAG, $"Excepcion: {fail.Message}"); Toast.MakeText(context, fail.Message, ToastLength.Long).Show(); _isStarted = false; return(_isStarted); // StartCommandResult.StickyCompatibility; } }); return(_isStarted); }