Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XAML
- <ContentPage.BindingContext>
- <vm:MapNavigarionVM />
- </ContentPage.BindingContext>
- <ContentPage.Content>
- <StackLayout>
- <Label Text="Where do you want to go"
- FontSize="Body"
- HorizontalTextAlignment="Center"
- Margin="0,30,0,0"/>
- <StackLayout Orientation="Horizontal">
- <Label Text="From: "
- Margin="30,30,0,0"/>
- <combobox:SfComboBox HeightRequest="40"
- HorizontalOptions="FillAndExpand"
- x:Name="NavigationFrom"
- SelectedItem="{x:Binding SelectedItemFrom}"
- DisplayMemberPath="street"
- IsEditableMode="False"
- Margin="0,0,30,0"/>
- </StackLayout>
- <StackLayout Orientation="Horizontal"
- Margin="0,30,0,0">
- <Label Text="To: "
- Margin="30,30,0,0" />
- <combobox:SfComboBox HeightRequest="40"
- IsEditableMode="False"
- HorizontalOptions="FillAndExpand"
- x:Name="NavigationTo"
- SelectedItem="{x:Binding SelectedItemTo}"
- DisplayMemberPath="street"
- Margin="0,0,30,0" />
- </StackLayout>
- </StackLayout>
- </ContentPage.Content>
- VM
- 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");
- }
- }
- }
- 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");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement