导航-检测跳转苹果自带地图、高德地图、百度地图、腾讯地图、谷歌地图

原文地址:https://blog.csdn.net/qq_30932479/article/details/86631065

1.info.plist中添加白名单,方便app跳转


image.png

2.创建一个新的类LSMapNavBase
并导入import CoreLocation
import MapKit
这里使用的朋友直接拷贝到项目中即可:

import UIKit
import CoreLocation
import MapKit

class LSMapNavBase: NSObject {
    //地图类型
    enum MapForm {
        enum MapURI:String {
            //百度
            case baiduMap = "baidumap://"
            //高德
            case gaodeMap = "iosamap://"
            //苹果
            case appleMap = "http://maps.apple.com/"
            //谷歌
            case googleMap = "comgooglemaps://"
            //腾讯
            case qqMap = "qqmap://"
        }
        enum MapName:String {
            //百度
            case baiduMap = "百度地图"
            //高德
            case gaodeMap = "高德地图"
            //苹果
            case appleMap = "系统地图"
            //谷歌
            case googleMap = "谷歌地图"
            //腾讯
            case qqMap = "腾讯地图"
        }
    }
    
    //地图信息
    struct MapInfo {
        static func baiDuUrlString(targetLat:Double,targetLon:Double,targetName:String) -> String {
            //return "baidumap://map/direction?origin={{我的位置}}&destination=\(targetName)&mode=driving&src=\(targetName)&coord_type=gcj02"
            /*
            注意:此处destination后面有三种写法:
            1,destination=\(targetLat),\(targetLon)
            2,destination=\(targetName)
            3,destination=name:\(targetName)|latlng:\(targetLat),\(targetLon)
            
            */
            return "baidumap://map/direction?origin={{我的位置}}&destination=name:\(targetName)|latlng:\(targetLat),\(targetLon)&mode=driving&src=\(targetName)&coord_type=gcj02"
        }
        static func gaoDeUrlString(targetLat:Double,targetLon:Double,targetName:String) -> String {
            return "iosamap://path?sourceApplication=导航功能&backScheme=\(appDisplayName)&poiname=\(targetName)&poiid=BGVIS&lat=\(targetLat)&lon=\(targetLon)&dname=\(targetName)&dev=0&m=0"

        }
        static func googleUrlString(targetLat:Double,targetLon:Double,targetName:String) -> String {
            return "comgooglemaps://?x-source=\(appDisplayName)&x-success=urlSchem&saddr=&daddr=targetLat,targetLon&directionsmode=driving"
        }
        static func qqDuUrlString(targetLat:Double,targetLon:Double,targetName:String) -> String {
            return "qqmap://routeplan?type=bus&from=&fromcoord=&to=\(targetName)&tocoord=\(targetLat),\(targetLon)&policy=1&referer=\(appDisplayName)"
        }
    }
    
    //检测地图是否存在然后打开
    struct CCMapGuide {
        static func judgeMapAppInPhoneAndJumpInto(targetLat:Double,targetLong:Double,targetName:String,VC:UIViewController) {
            //盛放地图元素的数组
            var maps = [[String: String]]()
            //判断地图
            //自带地图
            if UIApplication.shared.canOpenURL(URL(string: MapForm.MapURI.appleMap.rawValue)!) {
                var iosMap = [String: String]()
                iosMap["title"] = MapForm.MapName.appleMap.rawValue
                maps.append(iosMap)
            }
            //百度地图
            if UIApplication.shared.canOpenURL(URL(string: MapForm.MapURI.baiduMap.rawValue)!) {
                var baiduDic = [String: String]()
                baiduDic["title"] = MapForm.MapName.baiduMap.rawValue
                //\(self.pointDetailView.pointTitle.text ?? "")
                let urlString = MapInfo.baiDuUrlString(targetLat: targetLat, targetLon: targetLong, targetName: targetName)
                baiduDic["url"] = urlString
                maps.append(baiduDic)
            }
            //高德地图
            if UIApplication.shared.canOpenURL(URL(string: MapForm.MapURI.gaodeMap.rawValue)!) {
                var gaodeDic = [String: String]()
                gaodeDic["title"] = MapForm.MapName.gaodeMap.rawValue
                let urlString = MapInfo.gaoDeUrlString(targetLat: targetLat, targetLon: targetLong, targetName: targetName)
                let escapedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                gaodeDic["url"] = escapedString
                maps.append(gaodeDic)
            }
            //谷歌地图
            if UIApplication.shared.canOpenURL(URL(string: MapForm.MapURI.googleMap.rawValue)!) {
                var googleDic = [String: String]()
                googleDic["title"] = MapForm.MapName.googleMap.rawValue
                let urlString = MapInfo.googleUrlString(targetLat: targetLat, targetLon: targetLong, targetName: targetName)
                let escapedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                googleDic["url"] = escapedString
                maps.append(googleDic)
            }
            //腾讯地图
            if UIApplication.shared.canOpenURL(URL(string: MapForm.MapURI.qqMap.rawValue)!) {
                var qqDic = [String: String]()
                qqDic["title"] = MapForm.MapName.qqMap.rawValue
                let urlString = MapInfo.qqDuUrlString(targetLat: targetLat, targetLon: targetLong, targetName: targetName)
                let escapedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                qqDic["url"] = escapedString
                maps.append(qqDic)
            }
            if maps.count == 0 {
                return
            }
            let alertVC = UIAlertController.init(title: "请选择导航应用程序", message: nil, preferredStyle: .actionSheet)
            for i in 0..

注意:appDisplayName是宏定义如下:
let appDisplayName = JSON(infoDictionary)["CFBundleDisplayName"].stringValue

3.调用

                LSMapNavBase.CCMapGuide.judgeMapAppInPhoneAndJumpInto(targetLat: 31.248499, targetLong: 121.360095, targetName: "上海市金沙江路3131号", VC: self.controller!) // self.controller为当前视图控制器,我是在model类调用的,如果是控制器,直接self传过去。也可以直接写当前视图控制器的类名

你可能感兴趣的:(导航-检测跳转苹果自带地图、高德地图、百度地图、腾讯地图、谷歌地图)