Swift_跳转第三方地图导航

github地址
百度地图文档
高德地图文档

一.说明

在使用地图的时候,需要用到定位系统,有定位就要有坐标系。 但是不同的地图之间坐标系之间是不兼容的。所以用之前要做好处理。

  • WGS-84坐标系 (原始坐标系)

    用国际GPS纪录仪记录下来的经纬度,通过GPS定位拿到的原始经纬度,Google和高德地图定位的的经纬度(国外)都是基于WGS-84坐标系的;但是在国内是不允许直接用WGS84坐标系标注的,必须经过加密后才能使用。

  • GCJ-02坐标系 (火星坐标系)

    是我国独创的坐标体系。由WGS-84加密而成,在国内,必须至少使用GCJ-02坐标系,高德和Google在国内都是使用GCJ-02坐标系。或者使用在GCJ-02加密后再进行加密的坐标系,比如百度坐标系。

  • bd-09坐标系 (百度坐标系)

    百度坐标系是在GCJ-02坐标系的基础上再次加密偏移后形成的坐标系,只适用于百度地图。

二.代码部分

1. 白名单注册

跳转到外部app需要在白名单中注册对应app的scheme。在info.plist中添加LSApplicationQueriesSchemes。

  • 百度地图的scheme: baidumap

  • 高德地图的scheme: iosamap

  • 腾讯地图的scheme: qqmap


    白名单注册

2. 代码部分

    @objc func jumpToMapClicked() {
        
        let latitute = 31.248499
        let longitute = 121.360095
        let endAddress = "上海市金沙江路3131号"
        
        let alter = UIAlertController.init(title: "请选择导航应用程序", message: "", preferredStyle: UIAlertControllerStyle.actionSheet)
        
        let cancle = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel) { (a) in
        }
        
        let action1 = UIAlertAction.init(title: "苹果地图", style: UIAlertActionStyle.default) { (b) in
            self.appleMap(lat: latitute, lng: longitute, destination: endAddress)
        }
        
        let action2 = UIAlertAction.init(title: "高德地图", style: UIAlertActionStyle.default) { (b) in
            self.amap(dlat: latitute, dlon: longitute, dname: endAddress, way: 0)
        }
        
        let action3 = UIAlertAction.init(title: "百度地图", style: UIAlertActionStyle.default) { (b) in
            
            self.baidumap(endAddress: endAddress, way: "driving", lat: latitute,lng: longitute)
        }
        
        alter.addAction(action1)
        alter.addAction(action2)
        alter.addAction(action3)
        alter.addAction(cancle)
        
        self.present(alter, animated: true, completion: nil)
    }
    
    
    // 打开苹果地图
    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
        MKMapItem.openMaps(with: [currentLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: "true"])
    }
    
    // 打开高德地图
    func amap(dlat:Double,dlon:Double,dname:String,way:Int) {
        let appName = "app的名字"
        
        let urlString = "iosamap://path?sourceApplication=\(appName)&dname=\(dname)&dlat=\(dlat)&dlon=\(dlon)&t=\(way)" as NSString
        
        if self.openMap(urlString) == false {
            print("您还没有安装高德地图")

        }
    }
    
    // 打开百度地图
    func baidumap(endAddress:String,way:String,lat:Double,lng:Double) {
        
        let coordinate = CLLocationCoordinate2DMake(lat, lng)
        
        // 将高德的经纬度转为百度的经纬度
        let baidu_coordinate = getBaiDuCoordinateByGaoDeCoordinate(coordinate: coordinate)
        
        let destination = "\(baidu_coordinate.latitude),\(baidu_coordinate.longitude)"
        
        
        let urlString = "baidumap://map/direction?" + "&destination=" + endAddress + "&mode=" + way + "&destination=" + destination
        
        let str = urlString as NSString
        
        if self.openMap(str) == false {
            print("您还没有安装百度地图")
        }
    }
    
    // 打开第三方地图
    private func openMap(_ urlString: NSString) -> Bool {

        let url = NSURL(string:urlString.addingPercentEscapes(using: String.Encoding.utf8.rawValue)!)

        if UIApplication.shared.canOpenURL(url! as URL) == true {
            UIApplication.shared.openURL(url! as URL)
            return true
        } else {
            return false
        }
    }
    
    // 高德经纬度转为百度地图经纬度
    // 百度经纬度转为高德经纬度,减掉相应的值就可以了。
    func getBaiDuCoordinateByGaoDeCoordinate(coordinate:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065)
    }

你可能感兴趣的:(Swift_跳转第三方地图导航)