我有一个需要街道地址的应用程序,必须打开一个操作表,其中包含与iPhone上安装的导航应用程序相对应的操作。轻按该操作将打开导航应用程序,并将目的地作为提供的街道地址。我找不到能做到这一点的教程,因此我需要一些指导。

类似于Facebook iOS应用在点击“获取路线”时所做的操作。

最佳答案

我已经在中实现了它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>

08-03 23:03
查看更多