Advertisement
eduardogr

Untitled

Jan 1st, 2021
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. XAML
  2.  
  3.   <ContentPage.BindingContext>
  4.         <vm:MapNavigarionVM />
  5.     </ContentPage.BindingContext>
  6.    
  7.     <ContentPage.Content>
  8.         <StackLayout>
  9.             <Label Text="Where do you want to go"
  10.                    FontSize="Body"
  11.                    HorizontalTextAlignment="Center"
  12.                    Margin="0,30,0,0"/>
  13.             <StackLayout Orientation="Horizontal">
  14.                  <Label Text="From: "
  15.                    Margin="30,30,0,0"/>
  16.                 <combobox:SfComboBox HeightRequest="40"
  17.                                      HorizontalOptions="FillAndExpand"
  18.                                      x:Name="NavigationFrom"
  19.                                      SelectedItem="{x:Binding SelectedItemFrom}"
  20.                                      DisplayMemberPath="street"
  21.                                      IsEditableMode="False"
  22.                                      Margin="0,0,30,0"/>
  23.             </StackLayout>
  24.             <StackLayout Orientation="Horizontal"
  25.                          Margin="0,30,0,0">
  26.                 <Label Text="To: "
  27.                        Margin="30,30,0,0" />
  28.                 <combobox:SfComboBox HeightRequest="40"
  29.                                      IsEditableMode="False"
  30.                                      HorizontalOptions="FillAndExpand"
  31.                                      x:Name="NavigationTo"
  32.                                      SelectedItem="{x:Binding SelectedItemTo}"
  33.                                      DisplayMemberPath="street"
  34.                                      Margin="0,0,30,0" />
  35.             </StackLayout>
  36.         </StackLayout>
  37.     </ContentPage.Content>
  38.  
  39.  
  40.  
  41. VM  
  42.  
  43. private Address _SelectedItemFrom;
  44.         public Address SelectedItemFrom {
  45.             get { return _SelectedItemFrom; }
  46.             set
  47.             {
  48.                 if (_SelectedItemFrom != value) {
  49.                     _SelectedItemFrom = value;
  50.                     RaisePropertyChanged();
  51.                     getCoordinatesAsync("from");
  52.                 }
  53.             }
  54.         }
  55.  
  56.  
  57.         private Address _SelectedItemTo;
  58.         public Address SelectedItemTo {
  59.             get { return _SelectedItemTo; }
  60.             set
  61.             {
  62.                 if (_SelectedItemTo != value) {
  63.                     _SelectedItemTo = value;
  64.                     RaisePropertyChanged();
  65.                     getCoordinatesAsync("to");
  66.                 }
  67.             }
  68.         }
  69.  
  70.         private async void getCoordinatesAsync(string ToOrFrom) {
  71.  
  72.             try {
  73.                 if (ToOrFrom.Equals("to")) {
  74.                     var address = $"{SelectedItemTo.street} , {SelectedItemTo.city} , {SelectedItemTo.state}";
  75.                     var locations = await Geocoding.GetLocationsAsync(address);
  76.                     var location = locations?.FirstOrDefault();
  77.                     if (location != null) {
  78.                         SelectedItemTo.Lat = location.Latitude;
  79.                         SelectedItemTo.lon = location.Longitude;
  80.                     }
  81.                 } else if (ToOrFrom.Equals("from")) {
  82.                     var address = $"{SelectedItemFrom.street} , {SelectedItemFrom.city} , {SelectedItemFrom.state}";
  83.                     var locations = await Geocoding.GetLocationsAsync(address);
  84.                     var location = locations?.FirstOrDefault();
  85.                     if (location != null) {
  86.                         SelectedItemFrom.Lat = location.Latitude;
  87.                         SelectedItemFrom.lon = location.Longitude;
  88.                     }
  89.                 }
  90.             } catch (Exception ex) {
  91.                 await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
  92.             }
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement