前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift 打开三方地图 腾讯地图、百度地图、高德地图、Apple 地图

Swift 打开三方地图 腾讯地图、百度地图、高德地图、Apple 地图

作者头像
菜菜不吃蔡
发布2020-09-11 14:37:29
3.5K0
发布2020-09-11 14:37:29
举报
文章被收录于专栏:编程语言

需求:点击按钮打开三方地图导航,未安装应用跳转下载页面

首先配置白名单

在info.plist 添加LSApplicationQueriesSchemes

baidumap

iosamap

qqmap

1.点击导航按钮

代码语言:javascript
复制
 // MARK: 点击导航按钮
    @objc func touchgoMap() {
        let coortitle = "目的地"
        let latitute = self.coordinate!.latitude
        let longitute = self.coordinate!.longitude
        //        "请选择导航应用程序"
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
        
        alert.addAction(UIAlertAction(title: "高德地图", style: .default, handler: { _ in
            self.amap(dlat: latitute, dlon: longitute, dname: coortitle, way: 0)
        }))
        
        
        alert.addAction(UIAlertAction(title: "百度地图", style: .default, handler: { _ in
            self.baidumap(endAddress: coortitle, way: "driving", lat: latitute,lng: longitute)
        }))
        
        
        alert.addAction(UIAlertAction(title: "腾讯地图", style: .default, handler: { _ in
            self.qqmap(endAddress: coortitle, way: "driving", lat: latitute,lng: longitute)
        }))
        
        alert.addAction(UIAlertAction(title: "Apple 地图", style: .default, handler: { _ in
            self.appleMap(lat:latitute , lng:longitute, destination: coortitle)
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        let curentVC: BaseVC = getCurrentVC() as! BaseVC
        curentVC.present(alert, animated: true, completion: nil)
        
    }

2.跳转三方应用导航

代码语言:javascript
复制
 // 打开苹果地图
    func appleMap(lat:Double,lng:Double,destination:String) {
        
        let loc = CLLocationCoordinate2DMake(lat, lng)
        let currentLocation = MKMapItem.forCurrentLocation()
        let toLocation = MKMapItem(placemark:MKPlacemark(coordinate:loc,addressDictionary:nil))
        toLocation.name = destination
        let boo =    MKMapItem.openMaps(with: [currentLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: NSNumber(value: true)])
        print(boo)

    }
    
    // 打开高德地图
    func amap(dlat:Double,dlon:Double,dname:String,way:Int) {
        let appName = "Your appName"
        
        let urlString = "iosamap://path?sourceApplication=\(appName)&dname=\(dname)&dlat=\(dlat)&dlon=\(dlon)&t=\(way)" as String
        
        if self.openMap(urlString) == false {
            print("您还没有安装高德地图")
             let urlString = "itms-apps://itunes.apple.com/app/id452186370"
            self.openURL(urlString: urlString)

        }
    }
    
    // 打开腾讯地图
       func qqmap(endAddress:String,way:String,lat:Double,lng:Double) {
           
           let urlString = "qqmap://map/routeplan?type=\(way)&from=&fromcoord=CurrentLocation&to=\(endAddress)&tocoord=\(lat),\(lng)&referer=腾讯需要申请APPkey"
           let str = urlString as String
           
           if self.openMap(str) == false {
               print("您还没有安装腾讯地图")
            let urlString = "itms-apps://itunes.apple.com/app/id481623196"
            self.openURL(urlString: urlString)
           }
       }
    
    // 打开百度地图
    func baidumap(endAddress:String,way:String,lat:Double,lng:Double) {
        
        let coordinate = CLLocationCoordinate2DMake(lat, lng)
        
        // 将高德的经纬度转为百度的经纬度
        let baiduCoordinate = getBaiDuCoordinateByGaoDeCoordinate(coordinate: coordinate)
        
        let destination = "\(baiduCoordinate.latitude),\(baiduCoordinate.longitude)"
        
        let urlString = "baidumap://map/direction?" + "&destination=" + endAddress + "&mode=" + way + "&destination=" + destination
        
        let str = urlString as String
        
        if self.openMap(str) == false {
            print("您还没有安装百度地图")
            let urlString = "itms-apps://itunes.apple.com/app/id452186370" as String
            self.openURL(urlString: urlString)
        }
    }
    
    // 打开第三方地图
    private func openMap(_ urlString: String) -> Bool {

         let urlstr =   urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
        guard let url = URL(string:urlstr) else {
            return false
        }
        if UIApplication.shared.canOpenURL(url) == true {
            self.openURL(urlString: urlString as String)
            return true
        } else {
            return false
        }
    }
  func openURL(urlString:String) {
        let urlstr =   urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
        
        guard let url = URL(string:urlstr) else {
            return
        }
        //根据iOS系统版本,分别处理
        if #available(iOS 10, *) {
            UIApplication.shared.open(url , options: [:],
                                      completionHandler: { (success) in
                                        debugLog(success)
            })
        } else {
            UIApplication.shared.openURL(url )
        }
    }
    
    // 高德经纬度转为百度地图经纬度
    // 百度经纬度转为高德经纬度,减掉相应的值就可以了。
    func getBaiDuCoordinateByGaoDeCoordinate(coordinate:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065)
    }

3.如果需要判断是否安装app

代码语言:javascript
复制
 canOpenUrl("iosamap://") //高德
 canOpenUrl("baidumap://") //百度
 canOpenUrl("qqmap://") //腾讯
代码语言:javascript
复制
  func canOpenUrl(_ urlString: NSString) -> Bool {
        let url = NSURL(string:urlString.addingPercentEscapes(using: String.Encoding.utf8.rawValue)!)
        
        if UIApplication.shared.canOpenURL(url! as URL) == true {
            
            return true
        } else {
            return false
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/09/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档