Open Apple Maps for navigation with destination na

2019-02-25 18:16发布

问题:

I am fetching location data (like the coordinates used below) from Google's Places API.

When linking to Apple Maps for navigation from my app, I am currently using:

https://maps.apple.com/?daddr=[latitude],[longitude]

When Maps opens, it presents the coordinates for a short amount of time before resolving to an address. What I want is for the name of the destination to appear instead of coordinates or an address. Since I am using React Native, I would prefer a javascript-only solution (knowing that this essentially restricts a solution to this question to a url).

I have tried combinations of other parameters listed here to no avail.

回答1:

Try with this function, you only need to pass the coordinates and place name, this works, I use this in several projects

static func openMapsAppWithLocation(coordinates:CLLocationCoordinate2D,placeName:String)
{
    let regionDistance:CLLocationDistance = 10000
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
    ]
    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = placeName
    mapItem.openInMaps(launchOptions: options)
}

Hope this helps