swift

IOS swift 调起地图导航(系统地图、百度地图、高德地图)

2019-03-26  本文已影响0人  笨小孩81

先进行LSApplicationQueriesSchemes配置


1553529612923.jpg

各种地图的 Scheme:
百度地图:baidumap
高德地图:iosamap
谷歌地图:comgooglemaps
腾讯地图:qqmap
我这只配置了两个

然后在按钮的响应事件里添加如下代码:

func go2Map(_ annotation:MAAnnotation) {
        func goToSystemMap(){//嵌套函数
            let currentLocation = MKMapItem.forCurrentLocation()
            let toLocation = MKMapItem.init(placemark: MKPlacemark.init(coordinate: annotation.coordinate, addressDictionary: nil))
            MKMapItem.openMaps(with: [currentLocation, toLocation],launchOptions: [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:true])
        }
        
        //======其他地图都没的话就直接用系统地图========
        if !UIApplication.shared.canOpenURL(URL.init(string: "baidumap://")!) && !UIApplication.shared.canOpenURL(URL.init(string: "iosamap://")!) {
            goToSystemMap()
            return
        }
        
        let alertController = UIAlertController(title: "选择导航地图", message: nil, preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction.init(title: "取消", style: .cancel) { (action) in
            
        }
        //==================系统地图============
        let appleAction = UIAlertAction(title: "系统地图", style: .default){ (action) in
            goToSystemMap()
        }
        //=================百度地图=============
        if UIApplication.shared.canOpenURL(URL.init(string: "baidumap://")!) {//判断是否安装了地图
            let baiduAction = UIAlertAction(title: "百度地图", style: .default){ (action) in
                //origin={{我的位置}} ,目的地随便写
                let urlString = "baidumap://map/direction?origin={{我的位置}}&destination=latlng:\(annotation.coordinate.latitude),\(annotation.coordinate.longitude)|name=\(String(describing: annotation.title))&mode=driving&coord_type=gcj02"
                let escapedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                UIApplication.shared.openURL(URL.init(string: escapedString!)!)
            }
            alertController.addAction(baiduAction)
        }
        //高德地图
        if UIApplication.shared.canOpenURL(URL.init(string: "iosamap://")!) {
            let gaodeAction = UIAlertAction(title: "高德地图", style: .default){ (action) in
                let urlString = "iosamap://navi?sourceApplication=你应用的名字&backScheme=youappscheme&lat=\(annotation.coordinate.latitude)&lon=\(annotation.coordinate.longitude)&dev=0&style=2"
                let escapedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                UIApplication.shared.openURL(URL.init(string: escapedString!)!)
            }
            alertController.addAction(gaodeAction)
        }
        
        alertController.addAction(cancelAction)
        alertController.addAction(appleAction)
        
        present(alertController, animated: true, completion: nil)

    }
上一篇 下一篇

猜你喜欢

热点阅读