iOS之Swift实现调用第三方地图应用导航

iOS之Swift实现调用第三方地图应用导航

开始的一些话

作为一个“半路出家”的iOS的程序猿,一直使用OC开发,也想过使用Swift,但一直没有时间和机会;但最近做了一个新的项目,为我提供了机会,整个App大部分使用了Swift进行编写。体会就是Swift是真的很简练,入门也比较容易。
现在开发基本完成,于是准备把这次开发中用到的一些知识总结出来,写的不好,还请见谅!

第二个是Swift实现调用第三方地图应用导航,实现了调用Apple地图、百度地图腾讯地图高德地图

关于坐标系

美国GPS使用的是WGS84的坐标系统,以经纬度的形式来表示地球平面上的某一个位置,这种坐标系成为地球坐标。
在我国,出于国家安全考虑,国内所有导航电子地图必须使用国家测绘局制定的加密坐标系统,即将一个真实的经纬度坐标加密成一个不正确的经纬度坐标,称之为火星坐标GCJ02。
此外,百度地图在GCJ02坐标系基础上再次加密,其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标。

WGS84:大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。
GCJ02:又称火星坐标系,是中国国家测绘局制定的地理坐标系统,由WGS84加密后得到。
BD09:为百度坐标系,在GCJ02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标。

国内主流地图API使用的坐标系统:

  • 百度:百度坐标
  • 腾讯:火星坐标
  • 高德:火星坐标

源码

import UIKit
import MapKit

class MyNavi: NSObject, MAMapViewDelegate, AMapLocationManagerDelegate {
    // MARK: - 属性
    static let sharedInstance = MyNavi()
    
    // 当前位置或起始位置
    var currentLocation: CLLocationCoordinate2D!
    var currentAddress: String = ""
    
    // 目标位置
    var targetLocation: CLLocationCoordinate2D!
    var targetAddress: String = ""
    
    // MARK: - 导航位置初始化
    func initNavi(currentLoc: CLLocationCoordinate2D! = nil, currentAdd: String = "", targetLoc: CLLocationCoordinate2D!, targetAdd: String) {
        if currentLoc != nil {
            currentLocation = currentLoc
        }
        
        currentAddress = currentAdd
        
        if targetLoc != nil {
            targetLocation = targetLoc
        }
        
        targetAddress = targetAdd
    }
    
    // MARK: - Apple地图导航
    func naviWithAppleMap() {
        if targetLocation == nil {
            return
        }
        
        // 起始位置
        let fromLocation = MKMapItem.forCurrentLocation()
        fromLocation.name = "我的位置"
        
        // 目标位置
        let toCoor = CLLocationCoordinate2D(latitude: targetLocation!.latitude, longitude: targetLocation!.longitude)
        let toLocation = MKMapItem(placemark: MKPlacemark(coordinate: toCoor, addressDictionary: [:]))
        toLocation.name = targetAddress
        
        // 打开地图
        MKMapItem.openMaps(with: [fromLocation, toLocation],
                           launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: NSNumber(true)])
    }
    
    // MARK: - 百度地图导航
    func naviWithBaiduMap() {
        if targetLocation == nil {
            return
        }
        
        /// 请求格式:
        /// baidumap://map/direction?origin=34.264642646862,108.95108518068&destination=40.007623,116.360582&coord_type=bd09ll&mode=driving&src=ios.baidu.openAPIdemo
        
        var urlString: String = "baidumap://map/direction?"
        
        // 起始位置
        let origin: String = "我的位置"
        urlString = urlString + "origin=\(origin)"
        
        // 目标位置
        let destination: String = "\(targetLocation!.latitude),\(targetLocation!.longitude)"
        let name: String = targetAddress
        urlString = urlString + "&destination=name:\(name)|latlng:\(destination)"
        
        /// 可选的坐标系统:
        ///     bd09ll(百度经纬度坐标)
        ///     bd09mc(百度墨卡托坐标)
        ///     gcj02(经国测局加密的坐标)
        ///     wgs84(gps获取的原始坐标)
        urlString = urlString + "&coord_type=gcj02&mode=driving&src=ios.ftsafe.FTSmartSudentCard"

        urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
        
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: urlString)!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(URL(string: urlString)!)
        }
    }
    
    // MARK: - 腾讯地图导航
    func naviWithQqMap() {
        if targetLocation == nil {
            return
        }
        
        /// 请求格式:
        /// qqmap://map/routeplan?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
        /// 这里可能需要开发者key,即referer
        
        var urlString: String = "qqmap://map/routeplan?type=drive"
        let origin: String = "我的位置"
        urlString = urlString + "&from=\(origin)&fromcoord=CurrentLocation"
        
        let destination: String = "\(targetLocation!.latitude),\(targetLocation!.longitude)"
        let name: String = targetAddress
        urlString = urlString + "&to=\(name)&tocoord=\(destination)"
        
        urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
        
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: urlString)!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(URL(string: urlString)!)
        }
    }
    
    // MARK: - 高德地图导航
    func naviWithAMap() {
        if targetLocation == nil {
            return
        }
        
        /// 请求格式:
        /// iosamap://path?sourceApplication=applicationName&sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0
        
        var urlString: String = "iosamap://path?sourceApplication=FTSmartSudentCard"
        
        let name: String = targetAddress
        urlString = urlString + "&dlat=\(targetLocation!.latitude)&dlon=\(targetLocation!.longitude)&dname=\(name)&dev=0&t=0"
        
        urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
        
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: urlString)!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(URL(string: urlString)!)
        }
    }
}

调用方法

 class func naviEvent(coor: CLLocationCoordinate2D!, address: String) {
 	MyNavi.sharedInstance.initNavi(targetLoc: coor, targetAdd: address)
 	MyNavi.sharedInstance.naviWithAppleMap()
 	//MyNavi.sharedInstance.naviWithBaiduMap()
 	//MyNavi.sharedInstance.naviWithQqMap()
 	//MyNavi.sharedInstance.naviWithAMap()
 }

一点注意事项

如果要调用Apple地图导航,项目一定记得引入MapKit.framework

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二流人物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值