如何直接从应用程序打开苹果地图?

问题描述 投票:0回答:2

我正在尝试使用此代码直接从应用程序打开苹果地图

let url =  "http://maps.apple.com/maps?saddr=&daddr=\(latString),\(longString)"             
UIApplication.shared.open(URL(string: "\(url)")!,options: [:],
completionHandler: nil)

但它首先打开“SAFARI”,然后移动到苹果地图。问题是,从地图我无法返回到我的应用程序。顶部的后退按钮只能返回 safari。

有没有办法直接打开苹果地图?或者我必须使用谷歌地图吗?

编辑:答案是正确的,但这是更好的答案

let mapsURL = URL(string: "maps://?saddr=\(latStringS),\(longStringS)&daddr=\(latStringD),\(longStringD)")!

this will show direction to destination from starting location
swift apple-maps
2个回答
4
投票

通用解决方案

您应该将应用程序的深层链接作为 URL 传递以直接打开它们。对于

Maps
,这是您要查找的 URL:

let mapsURL = URL(string: "maps://q?\(latString),\(longString)")!

具体功能

此外,地图还有一个特定的功能,那就是:

class func openMaps(with mapItems: [MKMapItem], launchOptions: [String : Any]? = nil) -> Bool

您可以在文档

中找到更多相关信息

具体框架

另外,你可以使用我的框架来实现这个。支持 AppleMaps、GoogleMaps、Waze 和 Maps.Me。

你可以从这里获取


0
投票

您还可以使用

MapKit
的导入,您可以通过以下 API 更好地控制如何呈现地图:

    let latitude: CLLocationDegrees = 41
    let longitude: CLLocationDegrees = 52
    
    let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let placemark = MKPlacemark(coordinate: coordinate)
    let mapItem = MKMapItem(placemark: placemark)
    
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
    
    mapItem.openInMaps(launchOptions: launchOptions)
© www.soinside.com 2019 - 2024. All rights reserved.