【发布时间】:2017-05-24 05:44:03
【问题描述】:
我有一个应用程序,它需要一个街道地址,并且必须打开一个操作表,其中包含与 iPhone 上安装的导航应用程序相对应的操作。点击该操作会打开导航应用程序,其中目的地是提供的街道地址。我没有找到这样的教程,所以我需要一些指导。
类似于 Facebook iOS 应用在点击“获取路线”时所做的事情。
【问题讨论】:
标签: ios navigation maps directions
我有一个应用程序,它需要一个街道地址,并且必须打开一个操作表,其中包含与 iPhone 上安装的导航应用程序相对应的操作。点击该操作会打开导航应用程序,其中目的地是提供的街道地址。我没有找到这样的教程,所以我需要一些指导。
类似于 Facebook iOS 应用在点击“获取路线”时所做的事情。
【问题讨论】:
标签: ios navigation maps directions
我已经在 Swift 4
中实现了它为您要打开的所有导航应用定义一个枚举。
enum NavigationApps: String {
case appleMaps = "Maps"
case googleMaps = "Google Maps"
case hereWeGo = "HERE WeGo"
}
以下方法在位置元组中获取纬度和经度参数,并打开操作表,其中包含上述枚举中存在并安装在用户设备上的所有地图选项。 installedNavigationApps 是一个字典数组(键/值对),其中键来自 NavigationApps 枚举,值是它们各自的 URL 方案。
您需要做的就是在NavigationApps枚举中提及所有导航应用,在installedNavigationApps字典数组中提及URL方案,并在UIAlertAction处理程序中处理每个导航应用的应用启动。
import MapKit
extension UIViewController {
// MARK: - Map Navigation
func openMapForLocation(location: (latitude: CLLocationDegrees, longitude: CLLocationDegrees)) {
let installedNavigationApps : [[String:String]] = [[NavigationApps.appleMaps.rawValue:""], [NavigationApps.googleMaps.rawValue:"comgooglemaps://"], [NavigationApps.hereWeGo.rawValue:"here-route://"]]
var alertAction: UIAlertAction?
let alert = UIAlertController(title: "Select Navigation App", message: "Open in", preferredStyle: .actionSheet)
for app in installedNavigationApps {
let appName = app.keys.first
if (appName == NavigationApps.appleMaps.rawValue ||
appName == NavigationApps.googleMaps.rawValue || UIApplication.shared.canOpenURL(URL(string:app[appName!]!)!))
{
alertAction = UIAlertAction(title: appName, style: .default, handler: { (action) in
switch appName {
case NavigationApps.appleMaps.rawValue?:
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(location.latitude, location.longitude)
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 = "KIZAD"
mapItem.openInMaps(launchOptions: options)
break
case NavigationApps.googleMaps.rawValue?:
if UIApplication.shared.canOpenURL(URL(string:app[appName!]!)!) {
//open in Google Maps application
UIApplication.shared.open(URL(string:
"comgooglemaps://?saddr=&daddr=\(location.latitude),\(location.longitude)&directionsmode=driving")! as URL, options: [:], completionHandler: nil)
} else {
//open in Browser
let string = "https://maps.google.com/?q=@\(location.latitude),\(location.longitude)"
UIApplication.shared.open(URL(string: string)!)
}
break
case NavigationApps.hereWeGo.rawValue?:
UIApplication.shared.open(URL(string:
"here-route://mylocation/\(location.latitude),\(location.longitude)?ref=KIZAD&m=d")! as URL, options: [:], completionHandler: nil)
break
default:
break
}
})
alert.addAction(alertAction!)
}
else
{
print("Can't open URL scheme")
}
}
alertAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(alertAction!)
self.present(alert, animated: true, completion: nil)
}
}
重要提示: 不要忘记在 info.plist 中添加所有第三方导航应用的 URL 方案。例如:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
<string>here-route</string>
</array>
【讨论】:
有一个现有的库可以为您执行此操作。您需要按照说明将LSApplicationQueriesSchemes 键添加到信息列表中。
https://github.com/kiliankoe/Karte
截至本文撰写时,它支持:
【讨论】: