iOS开发笔记-141: swift5-跳转第三方导航app导航

func mapGoWeb(type: Int) {
        let longitudeS: String = viewM?.longitude ?? ""//经度
        let latitudeS: String = viewM?.latitude ?? ""//纬度
        let shop_nameS: String = viewM?.shop_name ?? ""//商家名称
        
        var routeUrlS: String = ""
        var openUrlS: String = ""
        var downloadUrlS: String = ""
        switch type {
        case 1://高德
            openUrlS = "iosamap://"

            routeUrlS = "iosamap://path?sourceApplication=ios.blackfish.XHY&dlat=\(latitudeS)&dlon=\(longitudeS)&dname=\(shop_nameS)&style=2"
                                
            downloadUrlS = "itms-apps://itunes.apple.com/app/id461703208?mt=8"
        case 2://百度
            openUrlS = "baidumap://"

            let tolocation = self.getBaiDuCoordinateByGaoDeCoordinate(coordinate: CLLocationCoordinate2D.init(latitude: Double(latitudeS)!, longitude: Double(longitudeS)!))
            routeUrlS = "baidumap://map/direction?origin=我的位置&destination=latlng:\(tolocation.latitude),\(tolocation.longitude)|name:\(shop_nameS)&mode=driving&coord_type=bd09ll&src=net.zoneland.m.o2oa"
                                
            downloadUrlS = "itms-apps://itunes.apple.com/cn/app/id452186370?mt=8"
        case 3://腾讯
            openUrlS = "qqmap://"

            routeUrlS = "qqmap://map/routeplan?type=drive&from=我的位置&to=\(shop_nameS)&tocoord=\(latitudeS),\(longitudeS)&policy=1"
            
            downloadUrlS = "itms-apps://itunes.apple.com/app/id481623196?mt=8"
        case 4://苹果
            let currentLocation = MKMapItem.forCurrentLocation()
            let tolocation = MKMapItem.init(placemark: MKPlacemark.init(coordinate: CLLocationCoordinate2D.init(latitude: Double(latitudeS)!, longitude: Double(longitudeS)!)))
            tolocation.name = shop_nameS
            let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
            MKMapItem.openMaps(with: [currentLocation,tolocation], launchOptions: options)
            return
        default: break
        }
        guard !routeUrlS.isEmpty else {
            return
        }
        
        
        if UIApplication.shared.canOpenURL(openUrlS.url!) {
            let url: URL = routeUrlS.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)?.url ?? URL(fileURLWithPath: "")
            UIApplication.shared.open(url, options: [:]) { (success) in
               if (success) {
                    print("跳转url")
               }else{
                    print("不能完成跳转")
               }
            }
        } else {
            UIApplication.shared.open(downloadUrlS.url!, options: [:]) { (success) in
               if (success) {
                    print("跳转url")
               }else{
                    print("不能完成跳转")
               }
            }
        }
    }

// 高德经纬度转为百度地图经纬度
    // 百度经纬度转为高德经纬度,减掉相应的值就可以了。
    func getBaiDuCoordinateByGaoDeCoordinate(coordinate:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065)
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容