/// <summary> /// Initializes a new instance of the <see cref="T:Locator.Portable.ViewModels.MapPageViewModel"/> class. /// </summary> /// <param name="navigation">Navigation.</param> /// <param name="geolocator">Geolocator.</param> /// <param name="commandFactory">Command factory.</param> /// <param name="geocodingWebServiceController">Geocoding repository.</param> public MapPageViewModel(INavigationService navigation, IGeolocator geolocator, Func <Action, ICommand> commandFactory, IGeocodingWebServiceController geocodingWebServiceController) : base(navigation) { _geolocator = geolocator; _geocodingWebServiceController = geocodingWebServiceController; _nearestAddressCommand = commandFactory(() => FindNearestSite()); _geolocationCommand = commandFactory(() => { if (_geolocationUpdating) { geolocator.Stop(); } else { geolocator.Start(); } GeolocationButtonTitle = _geolocationUpdating ? "Start" : "Stop"; _geolocationUpdating = !_geolocationUpdating; }); _positions = new List <IPosition> (); LocationUpdates = new Subject <IPosition> (); ClosestUpdates = new Subject <IPosition> (); }
/// <summary> /// Finds the nearest site. /// </summary> private void FindNearestSite() { if (_geolocationUpdating) { _geolocationUpdating = false; _geolocator.Stop(); GeolocationButtonTitle = "Start"; } double mindif = 99999; IPosition closest = null; var closestIndex = 0; var index = 0; if (_currentPosition != null) { foreach (var position in _positions) { var difference = PythagorasEquirectangular(_currentPosition.Latitude, _currentPosition.Longitude, position.Latitude, position.Longitude); if (difference < mindif) { closest = position; closestIndex = index; mindif = difference; } index++; } if (closest != null) { var array = _addresses[closestIndex]; Address = string.Format("{0}, {1}, {2}", array[0], array[1], array[2]); ClosestUpdates.OnNext(closest); } } }