async Task <bool> GeoLocation(string location) { locationList.Clear(); GeoResult result = await rest.GetGeoResult(location); if (result != null) { if (result.results.Count > 0) { foreach (var item in result.results) { var cell = new TextCell() { Text = item.formatted_address, }; cell.Tapped += (sender, e) => { double lat = item.geometry.location.lat; double lng = item.geometry.location.lng; if (isHomeSelected) { parent.originPos = new Position(lat, lng); parent.origin = item.formatted_address; parent.isHomeSet = 2; } else { parent.destinationPos = new Position(lat, lng); parent.destination = item.formatted_address; parent.isDestinationSet = 2; } Navigation.PopModalAsync(); }; locationList.Add(cell); } return(true); } else { Debug.WriteLine("Could not get info of home address"); return(false); } } else { Debug.WriteLine("Geocoder returns no results"); return(false); } }
public BreatheKlerePage() { InitializeComponent(); // MapTypes var mapTypeValues = new List <MapType>(); rest = new RESTService(); foreach (var mapType in Enum.GetValues(typeof(MapType))) { mapTypeValues.Add((MapType)mapType); } if (App.Current.Properties.ContainsKey("DID")) { DID = App.Current.Properties["DID"].ToString(); } isFirstLaunch = true; map.MapType = mapTypeValues[0]; map.MyLocationEnabled = true; map.IsTrafficEnabled = true; map.IsIndoorEnabled = false; map.UiSettings.CompassEnabled = true; map.UiSettings.RotateGesturesEnabled = true; map.UiSettings.MyLocationButtonEnabled = true; map.UiSettings.IndoorLevelPickerEnabled = false; map.UiSettings.ScrollGesturesEnabled = true; map.UiSettings.TiltGesturesEnabled = false; map.UiSettings.ZoomControlsEnabled = true; map.UiSettings.ZoomGesturesEnabled = true; var entryGesture = new TapGestureRecognizer(); entryGesture.Tapped += Home_Focused; entryAddress.GestureRecognizers.Add(entryGesture); var destinationGesture = new TapGestureRecognizer(); destinationGesture.Tapped += Destination_Focused; destinationAddress.GestureRecognizers.Add(destinationGesture); startPin = new Pin { Type = PinType.SavedPin, Label = "Start Point", }; endPin = new Pin { Type = PinType.Generic, Label = "End Point", }; // Map Clicked map.MapClicked += async(sender, e) => { if (mapMode == 2) { destinationPos = e.Point; endPin.Position = e.Point; GeoResult result = await rest.GetGeoResult(e.Point.Latitude.ToString() + ',' + e.Point.Longitude.ToString()); if (result != null) { destinationAddress.Text = result.results[0].formatted_address; destination = result.results[0].formatted_address; endPin.Address = result.results[0].formatted_address; } if (map.Pins.Contains(endPin)) { map.Pins.Remove(endPin); } map.Pins.Add(endPin); isDestinationSet = 2; await CalculateRoute(); } else if (mapMode == 1) { originPos = e.Point; startPin.Position = e.Point; GeoResult result = await rest.GetGeoResult(e.Point.Latitude.ToString() + ',' + e.Point.Longitude.ToString()); if (result != null) { origin = result.results[0].formatted_address; entryAddress.Text = result.results[0].formatted_address; startPin.Address = result.results[0].formatted_address; } if (map.Pins.Contains(startPin)) { map.Pins.Remove(startPin); } map.Pins.Add(startPin); isHomeSet = 2; } mapMode = 0; }; }
async protected override void OnAppearing() { base.OnAppearing(); NavigationPage.SetHasNavigationBar(this, false); try { var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (status != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location)) { await DisplayAlert("Need location", "Gunna need that location", "OK"); } var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location); //Best practice to always check that the key exists if (results.ContainsKey(Permission.Location)) { status = results[Permission.Location]; } } if (status == PermissionStatus.Granted) { if (Utils.IsLocationAvailable() && isFirstLaunch) { originPos = await Utils.GetPosition(); currentPos = originPos.Latitude + "," + originPos.Longitude; map.MoveToRegion(MapSpan.FromCenterAndRadius(originPos, Distance.FromMeters(5000))); GeoResult result = await rest.GetGeoResult(currentPos); if (result != null) { origin = result.results[0].formatted_address; } else { origin = currentPos; } isHomeSet = 2; } } else { await DisplayAlert("Location Denied", "Can not continue, try again.", "OK"); } if (isFirstLaunch) { isFirstLaunch = false; } } catch (Exception ex) { await DisplayAlert("Error", ex.Message, "OK"); } //Setting up the locations if (isHomeSet > 0) { if (isHomeSet == 1) { var result = await rest.GetGeoResult(origin); if (result != null) { originPos = new Position(result.results[0].geometry.location.lat, result.results[0].geometry.location.lng); } } entryAddress.Text = origin; startPin.Address = origin; if (map.Pins.Contains(startPin)) { map.Pins.Remove(startPin); } startPin.Position = originPos; map.Pins.Add(startPin); } if (isDestinationSet > 0) { if (isDestinationSet == 1) { var result = await rest.GetGeoResult(destination); if (result != null) { destinationPos = new Position(result.results[0].geometry.location.lat, result.results[0].geometry.location.lng); } } destinationAddress.Text = destination; endPin.Address = destination; if (map.Pins.Contains(endPin)) { map.Pins.Remove(endPin); } endPin.Position = destinationPos; map.Pins.Add(endPin); } if (isHomeSet > 0 && isDestinationSet > 0) { await CalculateRoute(); } }