How to get Apple Maps App (not MapKit) to add an annotation?

て烟熏妆下的殇ゞ 提交于 2019-12-31 03:57:29

问题


It's quite easy to add an annotation to a MapKit view which is inside your app.

theMap: MKMapView!

let pa = MKPointAnnotation()
pa.title = "title!!" etc
theMap.addAnnotation(pa)

But how the heck do you make the Maps App add an annotation??

Here's exactly how to open the Maps App to a certain address..

func appleMapApp() {
    quickAddressText = "123 Smith St 90210"
    let bottomText = "This shows at BOTTOM Of Maps App screen."

    let g = CLGeocoder()
    g.geocodeAddressString(quickAddressText) { placemarks, error in

        if let found = placemarks?.first, let lok = found.location {

            let p = MKPlacemark(coordinate: lok.coordinate, addressDictionary: nil)

            let mapItem = MKMapItem(placemark: p)
            mapItem.name = bottomText

            mapItem.openInMaps(launchOptions: showing(location: lok))
        }
    }
}

// convenient function...
func showing(location: CLLocation, meters m: CLLocationDistance = 1000)->[String : Any]? {
    let cr = MKCoordinateRegionMakeWithDistance(location.coordinate, m,m)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: cr.center),
        MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: cr.span)
    ]
    return options
}

That's great - but how the heck do you pass in a MKPointAnnotation to the actual Maps app??

(Again - it's easy to add a MKPointAnnotation on a map view inside your own app.)

Any ideas? Is it actually possible??


回答1:


I think openInMaps limits you only to the five possible launch options. But I wonder if you could get Apple Maps to open in the original way, openURL and maps.apple.com/maps, as shown in this SO question. With the newest iOS versions, though, it seems you also need to register the URL you're using in your info.plist under "URL Types ... URL Schemes" or "LSApplicationQueriesSchemes". You might be able to pass an annotation as a parameter with the URL.



来源:https://stackoverflow.com/questions/43234808/how-to-get-apple-maps-app-not-mapkit-to-add-an-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!