swift3.0打开第三方地图APP进行导航

声明:本文技术核心来自swift iOS9之后,打开第三方地图导航,但由于版本升级,部分代码已经不能用了,所以才写了此博文,方便自查

1、建立新项目

2 、在项目属性TARGETS->Bulid Phases的Link Binary With Libraries中添加MapKit.framework

3、在info.plist添加LSApplicationQueriesSchemes,类型为Array,然后添加四个item,分别是:baidumap,iosamap,comgooglemaps,qqmap

4、创建全局变量

var optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

5、添加方法:

//参数是我自己加的,目的地的经纬度和标题,
func creatOptionMenu(lat: String,lng: String,title:String){
        
            let SHARE_APPLICATION = UIApplication.shared
            if(SHARE_APPLICATION.canOpenURL(NSURL(string:"qqmap://")! as URL) == true){
                let qqAction = UIAlertAction(title: "腾讯地图", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    let urlString = "qqmap://map/routeplan?from=我的位置&type=drive&tocoord=\(lat),\(lng)&to=\(title)&coord_type=1&policy=0"
                    //let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
                    //let url = URL(string: urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()))
                    let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
                    SHARE_APPLICATION.openURL(url!)
                    
                })
                optionMenu.addAction(qqAction)
            }
            
            if(SHARE_APPLICATION.canOpenURL(NSURL(string:"iosamap://")! as URL) == true){
                let gaodeAction = UIAlertAction(title: "高德地图", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    let urlString = "iosamap://navi?sourceApplication=app名&backScheme=iosamap://&lat=\(lat)&lon=\(lng)&dev=0&style=2"
                    //let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
                    let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
                    SHARE_APPLICATION.openURL(url!)
                })
                optionMenu.addAction(gaodeAction)
            }
            
            if(SHARE_APPLICATION.canOpenURL(NSURL(string:"comgooglemaps://")! as URL) == true){
                let googleAction = UIAlertAction(title: "Google地图", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    let urlString = "comgooglemaps://?x-source=app名&x-success=comgooglemaps://&saddr=&daddr=\(lat),\(lng)&directionsmode=driving"
                    //let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
                    let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
                    SHARE_APPLICATION.openURL(url!)
                    
                })
                optionMenu.addAction(googleAction)
            }
            
            let appleAction = UIAlertAction(title: "苹果地图", style: .default, handler: {
                (alert: UIAlertAction!) -> Void in
                let loc = CLLocationCoordinate2DMake(Double(lat)!, Double(lng)!)
                let currentLocation = MKMapItem.forCurrentLocation()
                let toLocation = MKMapItem(placemark:MKPlacemark(coordinate:loc,addressDictionary:nil))
                toLocation.name = title
                MKMapItem.openMaps(with: [currentLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: NSNumber(value: true)])
                
            })
            optionMenu.addAction(appleAction)
            
            if(SHARE_APPLICATION.canOpenURL(NSURL(string:"baidumap://")! as URL) == true){
                let baiduAction = UIAlertAction(title: "百度地图", style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    let urlString = "baidumap://map/direction?origin={{我的位置}}&destination=latlng:\(lat),\(lng)|name=\(title)&mode=driving&coord_type=gcj02"
                    //let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
                    let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
                    SHARE_APPLICATION.openURL(url!)
                    
                    
                })
                optionMenu.addAction(baiduAction)
            }
            
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: {
                (alert: UIAlertAction!) -> Void in
            })
            optionMenu.addAction(cancelAction)
        
        
        
    }

6、调用

在需要执行的方法体里调用以下代码:

creatOptionMenu(lat: latstr,lng: lngstr,title: titstr)
self.present(optionMenu, animated: true, completion: nil)
//要重置optionMenu
self.optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

如果在同一view需要多次调用,在调用之后需要重置optionMenu,否则会崩溃


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值