我有一个应用程序,它采用街道地址,并且必须打开一个操作表,其中包含与iPhone上安装的导航应用程序相对应的操作。点击操作会打开导航应用程序,并将目标作为提供的街道地址。我没有找到这样做的教程,所以我需要一些指导。
类似于Facebook iOS应用程序在点击时获取的方式"获取路线"。
答案 0 :(得分:1)
我已在 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>
答案 1 :(得分:0)
有一个现有的库可以为您执行此操作。您需要按照说明将LSApplicationQueriesSchemes
键添加到信息列表中。
https://github.com/kiliankoe/Karte
截至撰写时,它支持: