Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Address _SelectedItemFrom;
- public Address SelectedItemFrom {
- get { return _SelectedItemFrom; }
- set
- {
- if (_SelectedItemFrom != value) {
- _SelectedItemFrom = value;
- RaisePropertyChanged();
- getCoordinatesAsync("from");
- }
- }
- }
- private Address _SelectedItemTo;
- public Address SelectedItemTo {
- get { return _SelectedItemTo; }
- set
- {
- if (_SelectedItemTo != value) {
- _SelectedItemTo = value;
- RaisePropertyChanged();
- getCoordinatesAsync("to");
- }
- }
- }
- public ICommand StartNavigation { get; set; }
- public MapNavigarionVM() {
- StartNavigation = new Command(async () => {
- await Navigate();
- });
- }
- private async void getCoordinatesAsync(string ToOrFrom) {
- try {
- if (ToOrFrom.Equals("to")) {
- var address = $"{SelectedItemTo.street} , {SelectedItemTo.city} , {SelectedItemTo.state}";
- var locations = await Geocoding.GetLocationsAsync(address);
- var location = locations?.FirstOrDefault();
- if (location != null) {
- SelectedItemTo.Lat = location.Latitude;
- SelectedItemTo.lon = location.Longitude;
- }
- } else if (ToOrFrom.Equals("from")) {
- var address = $"{SelectedItemFrom.street} , {SelectedItemFrom.city} , {SelectedItemFrom.state}";
- var locations = await Geocoding.GetLocationsAsync(address);
- var location = locations?.FirstOrDefault();
- if (location != null) {
- SelectedItemFrom.Lat = location.Latitude;
- SelectedItemFrom.lon = location.Longitude;
- }
- }
- } catch (Exception ex) {
- await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
- }
- }
- public async Task Navigate() {
- var location = new Location(SelectedItemTo.lon, SelectedItemTo.Lat) ;
- var options = new MapLaunchOptions { NavigationMode = NavigationMode.Driving };
- await Map.OpenAsync(location, options);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement