internal static async Task PlatformOpenMapsAsync(Placemark placemark, MapsLaunchOptions options) { var address = new MKPlacemarkAddress { CountryCode = placemark.CountryCode, Country = placemark.CountryName, State = placemark.AdminArea, Street = placemark.Thoroughfare, City = placemark.Locality, Zip = placemark.PostalCode }; var coder = new CLGeocoder(); CLPlacemark[] placemarks = null; try { placemarks = await coder.GeocodeAddressAsync(address.Dictionary); } catch { Debug.WriteLine("Unable to get geocode address from address"); return; } if ((placemarks?.Length ?? 0) == 0) { Debug.WriteLine("No locations exist, please check address."); } await OpenPlacemark(new MKPlacemark(placemarks[0].Location.Coordinate, address), options); }
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); }
public async Task <bool> TryOpenAsync(Placemark placemark, MapLaunchOptions options) { if (placemark == null) { throw new ArgumentNullException(nameof(placemark)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } #if __IOS__ var address = new MKPlacemarkAddress { CountryCode = placemark.CountryCode, Country = placemark.CountryName, State = placemark.AdminArea, Street = placemark.Thoroughfare, City = placemark.Locality, Zip = placemark.PostalCode }.Dictionary; #else var address = new NSMutableDictionary { [CNPostalAddressKey.City] = new NSString(placemark.Locality ?? string.Empty), [CNPostalAddressKey.Country] = new NSString(placemark.CountryName ?? string.Empty), [CNPostalAddressKey.State] = new NSString(placemark.AdminArea ?? string.Empty), [CNPostalAddressKey.Street] = new NSString(placemark.Thoroughfare ?? string.Empty), [CNPostalAddressKey.PostalCode] = new NSString(placemark.PostalCode ?? string.Empty), [CNPostalAddressKey.IsoCountryCode] = new NSString(placemark.CountryCode ?? string.Empty) }; #endif var resolvedPlacemarks = await GetPlacemarksAsync(address); if (resolvedPlacemarks?.Length > 0) { return(await OpenPlacemark(new MKPlacemark(resolvedPlacemarks[0].Location.Coordinate, address), options)); } else { #if __IOS__ || __MACOS__ // https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html var uri = $"http://maps.apple.com/?q={placemark.GetEscapedAddress()}"; var nsurl = NSUrl.FromString(uri); return(await Launcher.Default.TryOpenAsync(nsurl)); #else return(await OpenPlacemark(new MKPlacemark(default, address), options));
void Directions_TouchUpInside(object sender, EventArgs e) { var location = new CLLocationCoordinate2D(this.Cinema.Latitude, this.Cinema.Longitute); MKPlacemarkAddress address = null; var placemark = new MKPlacemark(location, address); var mapItem = new MKMapItem(placemark); mapItem.Name = String.Format("Cineworld {0}", this.Cinema.Name); var launchOptions = new MKLaunchOptions(); launchOptions.DirectionsMode = (sender == this.btnWalkingDirections) ? MKDirectionsMode.Walking : MKDirectionsMode.Driving; launchOptions.ShowTraffic = (sender == this.btnDrivingDirections); launchOptions.MapType = MKMapType.Standard; mapItem.OpenInMaps(launchOptions); }
internal static async Task PlatformOpenMapsAsync(Placemark placemark, MapLaunchOptions options) { #if __IOS__ var address = new MKPlacemarkAddress { CountryCode = placemark.CountryCode, Country = placemark.CountryName, State = placemark.AdminArea, Street = placemark.Thoroughfare, City = placemark.Locality, Zip = placemark.PostalCode }.Dictionary; #else var address = new NSMutableDictionary { [CNPostalAddressKey.City] = new NSString(placemark.Locality ?? string.Empty), [CNPostalAddressKey.Country] = new NSString(placemark.CountryName ?? string.Empty), [CNPostalAddressKey.State] = new NSString(placemark.AdminArea ?? string.Empty), [CNPostalAddressKey.Street] = new NSString(placemark.Thoroughfare ?? string.Empty), [CNPostalAddressKey.PostalCode] = new NSString(placemark.PostalCode ?? string.Empty), [CNPostalAddressKey.IsoCountryCode] = new NSString(placemark.CountryCode ?? string.Empty) }; #endif var coder = new CLGeocoder(); CLPlacemark[] placemarks; try { placemarks = await coder.GeocodeAddressAsync(address); } catch { Debug.WriteLine("Unable to get geocode address from address"); return; } if ((placemarks?.Length ?? 0) == 0) { Debug.WriteLine("No locations exist, please check address."); } await OpenPlacemark(new MKPlacemark(placemarks[0].Location.Coordinate, address), options); }
/// <summary> /// Navigate to an address /// </summary> /// <param name="name">Label to display</param> /// <param name="street">Street</param> /// <param name="city">City</param> /// <param name="state">Sate</param> /// <param name="zip">Zip</param> /// <param name="country">Country</param> /// <param name="countryCode">Country Code if applicable</param> /// <param name="navigationType">Navigation type</param> public async void NavigateTo(string name, string street, string city, string state, string zip, string country, string countryCode, NavigationType navigationType = NavigationType.Default) { if (string.IsNullOrWhiteSpace(name)) { name = string.Empty; } if (string.IsNullOrWhiteSpace(street)) { street = string.Empty; } if (string.IsNullOrWhiteSpace(city)) { city = string.Empty; } if (string.IsNullOrWhiteSpace(state)) { state = string.Empty; } if (string.IsNullOrWhiteSpace(zip)) { zip = string.Empty; } if (string.IsNullOrWhiteSpace(country)) { country = string.Empty; } var placemarkAddress = new MKPlacemarkAddress { City = city, Country = country, State = state, Street = street, Zip = zip, CountryCode = countryCode }; var coder = new CLGeocoder(); var placemarks = await coder.GeocodeAddressAsync(placemarkAddress.Dictionary); if (placemarks.Length == 0) { Debug.WriteLine("Unable to get geocode address from address"); return; } CLPlacemark placemark = placemarks[0]; var mapItem = new MKMapItem(new MKPlacemark(placemark.Location.Coordinate, placemarkAddress)); mapItem.Name = name; MKLaunchOptions launchOptions = null; if (navigationType != NavigationType.Default) { launchOptions = new MKLaunchOptions { DirectionsMode = navigationType == NavigationType.Driving ? MKDirectionsMode.Driving : MKDirectionsMode.Walking }; } var mapItems = new[] { mapItem }; MKMapItem.OpenMaps(mapItems, launchOptions); }
/// <summary> /// Navigate to an address /// </summary> /// <param name="name">Label to display</param> /// <param name="street">Street</param> /// <param name="city">City</param> /// <param name="state">Sate</param> /// <param name="zip">Zip</param> /// <param name="country">Country</param> /// <param name="countryCode">Country Code if applicable</param> /// <param name="navigationType">Navigation type</param> public async Task <bool> NavigateTo(string name, string street, string city, string state, string zip, string country, string countryCode, NavigationType navigationType = NavigationType.Default) { if (string.IsNullOrWhiteSpace(name)) { name = string.Empty; } if (string.IsNullOrWhiteSpace(street)) { street = string.Empty; } if (string.IsNullOrWhiteSpace(city)) { city = string.Empty; } if (string.IsNullOrWhiteSpace(state)) { state = string.Empty; } if (string.IsNullOrWhiteSpace(zip)) { zip = string.Empty; } if (string.IsNullOrWhiteSpace(country)) { country = string.Empty; } CLPlacemark[] placemarks = null; MKPlacemarkAddress placemarkAddress = null; try { placemarkAddress = new MKPlacemarkAddress { City = city, Country = country, State = state, Street = street, Zip = zip, CountryCode = countryCode }; var coder = new CLGeocoder(); placemarks = await coder.GeocodeAddressAsync(placemarkAddress.Dictionary); } catch (Exception ex) { Debug.WriteLine("Unable to get geocode address from address: " + ex); return(false); } if ((placemarks?.Length ?? 0) == 0) { Debug.WriteLine("No locations exist, please check address."); return(false); } try { var placemark = placemarks[0]; var mapItem = new MKMapItem(new MKPlacemark(placemark.Location.Coordinate, placemarkAddress)); mapItem.Name = name; MKLaunchOptions launchOptions = null; if (navigationType != NavigationType.Default) { launchOptions = new MKLaunchOptions { DirectionsMode = navigationType == NavigationType.Driving ? MKDirectionsMode.Driving : MKDirectionsMode.Walking }; } var mapItems = new[] { mapItem }; MKMapItem.OpenMaps(mapItems, launchOptions); } catch (Exception ex) { Debug.WriteLine("Unable to launch maps: " + ex); } return(true); }
/// <summary> /// Navigate to an address /// </summary> /// <param name="name">Label to display</param> /// <param name="street">Street</param> /// <param name="city">City</param> /// <param name="state">Sate</param> /// <param name="zip">Zip</param> /// <param name="country">Country</param> /// <param name="countryCode">Country Code if applicable</param> /// <param name="navigationType">Navigation type</param> public async Task<bool> NavigateTo(string name, string street, string city, string state, string zip, string country, string countryCode, NavigationType navigationType = NavigationType.Default) { if (string.IsNullOrWhiteSpace(name)) name = string.Empty; if (string.IsNullOrWhiteSpace(street)) street = string.Empty; if (string.IsNullOrWhiteSpace(city)) city = string.Empty; if (string.IsNullOrWhiteSpace(state)) state = string.Empty; if (string.IsNullOrWhiteSpace(zip)) zip = string.Empty; if (string.IsNullOrWhiteSpace(country)) country = string.Empty; CLPlacemark[] placemarks = null; MKPlacemarkAddress placemarkAddress = null; try { placemarkAddress = new MKPlacemarkAddress { City = city, Country = country, State = state, Street = street, Zip = zip, CountryCode = countryCode }; var coder = new CLGeocoder(); placemarks = await coder.GeocodeAddressAsync(placemarkAddress.Dictionary); } catch (Exception ex) { Debug.WriteLine("Unable to get geocode address from address: " + ex); return false; } if ((placemarks?.Length ?? 0) == 0) { Debug.WriteLine("No locations exist, please check address."); return false; } try { var placemark = placemarks[0]; var mapItem = new MKMapItem(new MKPlacemark(placemark.Location.Coordinate, placemarkAddress)); mapItem.Name = name; MKLaunchOptions launchOptions = null; if (navigationType != NavigationType.Default) { launchOptions = new MKLaunchOptions { DirectionsMode = navigationType == NavigationType.Driving ? MKDirectionsMode.Driving : MKDirectionsMode.Walking }; } var mapItems = new[] { mapItem }; MKMapItem.OpenMaps(mapItems, launchOptions); } catch(Exception ex) { Debug.WriteLine("Unable to launch maps: " + ex); } return true; }
void FindRouteToSafeZone() { MKPlacemarkAddress address = null; //Start at our current location var fromLocation = new MKPlacemark(CDC.Coordinates, address); //Go to the safe zone var destinationLocation = new MKPlacemark(new CLLocationCoordinate2D(Cheyenne.Latitude, Cheyenne.Longitude), address); var request = new MKDirectionsRequest { Source = new MKMapItem(fromLocation), Destination = new MKMapItem(destinationLocation), RequestsAlternateRoutes = false }; var directions = new MKDirections(request); //Async network call to Apple's servers directions.CalculateDirections((response, error) => { if (error != null) { Console.WriteLine(error.LocalizedDescription); } else { foreach (var route in response.Routes) { map.AddOverlay(route.Polyline); } } }); map.OverlayRenderer = (mapView, overlay) => { if (overlay is MKCircle) { if (circleRenderer == null) { circleRenderer = new MKCircleRenderer(overlay as MKCircle) { FillColor = UIColor.Red, Alpha = 0.5f }; } return(circleRenderer); } else if (overlay is MKPolygon) { if (polyRenderer == null) { polyRenderer = new MKPolygonRenderer(overlay as MKPolygon); polyRenderer.FillColor = UIColor.Green; polyRenderer.Alpha = 0.5f; } return(polyRenderer); } else if (overlay is MKPolyline) { var route = (MKPolyline)overlay; var renderer = new MKPolylineRenderer(route) { StrokeColor = UIColor.Blue }; return(renderer); } return(null); }; Helpers.CenterOnUnitedStates(map); }
/// <summary> /// Navigate to an address /// </summary> /// <param name="name">Label to display</param> /// <param name="street">Street</param> /// <param name="city">City</param> /// <param name="state">Sate</param> /// <param name="zip">Zip</param> /// <param name="country">Country</param> /// <param name="countryCode">Country Code if applicable</param> /// <param name="navigationType">Navigation type</param> public async void NavigateTo(string name, string street, string city, string state, string zip, string country, string countryCode, NavigationType navigationType = NavigationType.Default) { if (string.IsNullOrWhiteSpace(name)) name = string.Empty; if (string.IsNullOrWhiteSpace(street)) street = string.Empty; if (string.IsNullOrWhiteSpace(city)) city = string.Empty; if (string.IsNullOrWhiteSpace(state)) state = string.Empty; if (string.IsNullOrWhiteSpace(zip)) zip = string.Empty; if (string.IsNullOrWhiteSpace(country)) country = string.Empty; var placemarkAddress = new MKPlacemarkAddress { City = city, Country = country, State = state, Street = street, Zip = zip, CountryCode = countryCode }; var coder = new CLGeocoder(); var placemarks = await coder.GeocodeAddressAsync(placemarkAddress.Dictionary); if(placemarks.Length == 0) { Debug.WriteLine("Unable to get geocode address from address"); return; } CLPlacemark placemark = placemarks[0]; var mapItem = new MKMapItem(new MKPlacemark(placemark.Location.Coordinate, placemarkAddress)); mapItem.Name = name; MKLaunchOptions launchOptions = null; if (navigationType != NavigationType.Default) { launchOptions = new MKLaunchOptions { DirectionsMode = navigationType == NavigationType.Driving ? MKDirectionsMode.Driving : MKDirectionsMode.Walking }; } var mapItems = new[] { mapItem }; MKMapItem.OpenMaps(mapItems, launchOptions); }