static Task DoLaunchDirections(NavigationAddress destination) { var url = $"{BASE_URL}{ToUrl(destination)}"; if (Attempt(url, "com.google.android.apps.maps", "com.google.android.maps.MapsActivity")) { return(Task.CompletedTask); } if (Attempt(url)) { return(Task.CompletedTask); } if (destination.HasGeoLocation()) { url = "geo:{0},{1}?q={0},{1}{2}".FormatWith( destination.Latitude, destination.Longitude, destination.Name.WithWrappers("(", ")")); } else { url = "geo:0,0?q=" + destination.GetAddressParts().ToString(" "); } if (Attempt(url)) { return(Task.CompletedTask); } throw new Exception(" There is no recognized map application."); }
static async Task <CLLocationCoordinate2D?> FindCoordinates(NavigationAddress address) { CLPlacemark[] placemarks = null; var placemarkAddress = new MKPlacemarkAddress { City = address.City.OrEmpty(), Country = address.Country.OrEmpty(), CountryCode = address.CountryCode.OrEmpty(), State = address.State.OrEmpty(), Street = address.Street.OrEmpty(), Zip = address.Zip.OrEmpty() }; try { using (var coder = new CLGeocoder()) placemarks = await coder.GeocodeAddressAsync(placemarkAddress.Dictionary); } catch (Exception ex) { throw new Exception("Failed to obtain geo Location from address: " + ex); } var result = placemarks?.FirstOrDefault()?.Location?.Coordinate; if (result == null) { throw new Exception("Failed to obtain geo Location from address."); } return(result); }
static string ToUrl(NavigationAddress address) { if (address.HasGeoLocation()) { return($"{address.Latitude},{address.Longitude}{address.Name.WithWrappers(" (", ")")}"); } else { var addr = address.GetAddressParts(); var location = GetLocationFromAddress(addr.ToString(",")); return(location.Latitude + "," + location.Longitude); } }
/// <summary> /// Will launch the external directions application and return whether it was successful. /// </summary> public static async Task <bool> LaunchDirections(NavigationAddress destination, OnError errorAction = OnError.Toast) { try { await AskForPermission(); await DoLaunchDirections(destination); return(true); } catch (Exception ex) { await errorAction.Apply(ex, "Launching navigation directions failed"); return(false); } }
static async Task DoLaunchDirections(NavigationAddress destination) { string query; if (destination.HasGeoLocation()) { query = $"collection=point.{destination.Latitude}_{destination.Longitude}_{destination.Name}"; } else { query = "where=" + destination.GetAddressParts().Select(x => x).ToString(","); } var successful = await Windows.System.Launcher.LaunchUriAsync(new Uri("bingmaps:?" + query)); if (!successful) { throw new Exception("Failed to launch BingMaps"); } }
static async Task DoLaunchDirections(NavigationAddress destination) { await Thread.UI.Run(async() => { CLLocationCoordinate2D?coords; if (destination.HasGeoLocation()) { coords = new CLLocationCoordinate2D(destination.Latitude.Value, destination.Longitude.Value); } else { coords = await FindCoordinates(destination); } using (var mark = new MKPlacemark(coords.Value, default(NSDictionary))) using (var mapItem = new MKMapItem(mark) { Name = destination.Name.OrEmpty() }) MKMapItem.OpenMaps(new[] { mapItem }); }); }