public void OnLocationChanged(Android.Locations.Location location) { MarkerOptions currentPos = new MarkerOptions(); currentPos.SetPosition(new LatLng(location.Latitude, location.Longitude)); currentPos.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueAzure)); currentPos.SetTitle("Your Position"); _currentLocationMarker?.Remove(); if (MMap != null) { _currentLocationMarker = MMap.AddMarker(currentPos); } foreach (var marker in _markerList) { if (Location.CalculateDistance(new Location(location.Latitude, location.Longitude), new Location(marker.Lat, marker.Lon), DistanceUnits.Kilometers) < marker.Dist) { if (!marker.Availability) { try { MailAddress from = new MailAddress("*****@*****.**", "XamarinApp"); MailAddress to = new MailAddress(user_email); MailMessage m = new MailMessage(from, to) { Subject = "Location Tracking", Body = $"<h2>You are near place named: {marker.Title}<h2>" + $"Your latitude is {location.Latitude}, your longitude is {location.Longitude}", IsBodyHtml = true }; var smtp = new SmtpClient("smtp.gmail.com") { Credentials = new NetworkCredential("oop.exam.xam", ""), EnableSsl = true }; // password is not pulic. smtp.Send(m); marker.Availability = true; } catch { // ignored } } } else if (marker.Availability) { marker.Availability = false; } } }
public static bool ShouldRefresh(Xamarin.Essentials.Location oldLocation, Xamarin.Essentials.Location newLocation) { if (newLocation == null) { return(false); } if (oldLocation == null) { return(true); } if (newLocation.Timestamp.Subtract(oldLocation.Timestamp) > TimeSpan.FromMinutes(Constants.MainMapWeatherRefreshDuration)) { return(true); } if (newLocation.CalculateDistance(oldLocation, DistanceUnits.Kilometers) > Constants.MainMapWeatherRefreshDistance) { return(true); } return(false); }
/// <summary> /// Get the distance from another postion in km /// </summary> /// @author Gabriele Ursini /// <returns> A distance </returns> public double getDistance(Xamarin.Essentials.Location newPosition) { return(position.CalculateDistance(newPosition, DistanceUnits.Kilometers)); }
/// <summary> /// Sends the user's location to the server if the distance is to the last request is larger than 10m, otherwise an exponential backoff occrus /// Backoff = (previous backoff ^ 2) /// </summary> protected async void Track() { bool success = false; int retry = 0; retry = 0; try { // TODO: // faster they go, the more locations sent and slower they go the less request = new GeolocationRequest(GeolocationAccuracy.Best); Xamarin.Essentials.Location location = null; if (Previous_request != null) { location = await Geolocation.GetLocationAsync(request); Models.Location customLocation = new Models.Location(location); if (!LocationService.LocationSavedToNotSend(customLocation)) { if (location.CalculateDistance(Previous_request, DistanceUnits.Kilometers) >= MetersConsidedUserMoved) { await customLocation.GetRegion(); _Backoff = Initial_Backoff; Track_Retry = 0; SendUserLocation(customLocation); } else { ExtendBackoff(); SendUserLocation(customLocation); } } else { ExtendBackoff(); } } else { location = await Geolocation.GetLocationAsync(request); Models.Location customLocation = new Models.Location(location); if ((!LocationService.LocationSavedToNotSend(customLocation))) { await customLocation.GetRegion(); SendUserLocation(customLocation); } } Previous_request = location; success = true; } catch (Exception e) { Debug.WriteLine(e.StackTrace); //retry for getting the geolocation retry++; } }
public static double CalculateDistance(this Location locationStart, Location locationEnd, DistanceUnits units) => Location.CalculateDistance(locationStart, locationEnd, units);
public static double CalculateDistance(this Location locationStart, double latitudeEnd, double longitudeEnd, DistanceUnits units) => Location.CalculateDistance(locationStart, latitudeEnd, longitudeEnd, units);