ios - 如何显示具有多个导航应用程序选项的操作表

标签 ios navigation maps directions

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

与 Facebook iOS 应用程序在点击“获取路线”时执行的操作类似。

最佳答案

我已经在 Swift 4 中实现了它

为您要打开的所有导航应用程序定义一个枚举。

enum NavigationApps: String {
    case appleMaps = "Maps"
    case googleMaps = "Google Maps"
    case hereWeGo = "HERE WeGo"
}

以下方法采用位置元组中的纬度和经度参数,并打开操作表,其中包含上面枚举中存在并安装在用户设备上的所有 map 的选项。 installedNavigationApps 是一个字典数组(键/值对),其中键来自 NavigationApps 枚举,值是它们各自的 URL 方案。

您需要做的就是在 NavigationApps 枚举中提及所有导航应用程序,在 installedNavigationApps 字典数组中提及 URL 方案,并在 installedNavigationApps 数组中处理每个导航应用程序的应用程序启动 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>

关于ios - 如何显示具有多个导航应用程序选项的操作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149894/

相关文章:

r - 处理用 ggplot2 绘制的等值线图中的异常值

r - ggplot2/gis 在多边形区域内绘图

html - 有没有办法强制水平导航菜单中的多字 li 保持在一行?

jquery - 响应式导航栏在打开时关闭

java - 如何获取道路的宽度

ios - 我如何为 IOS 导出我的 Flex 项目

ios - NSInvalidArgumentException 原因 : 'Storyboard doesn' t contain a view controller with identifier 'CenterViewController' '

ios - 从特定的 UIView 隐藏标签?

ios - 表达式类型 '()' 在没有更多上下文的情况下不明确

css - 基于图像的 CSS 下拉菜单