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 MapKitclass 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 = currentAddif 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.openAPIdemovar urlString: String = "baidumap://map/direction?"// 起始位置let origin: String = "我的位置"urlString = urlString + "origin=\(origin)"// 目标位置let destination: String = "\(targetLocation!.latitude),\(targetLocation!.longitude)"let name: String = targetAddressurlString = 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,即referervar 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 = targetAddressurlString = 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=0var urlString: String = "iosamap://path?sourceApplication=FTSmartSudentCard"let name: String = targetAddressurlString = 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

iOS之Swift实现调用第三方地图应用导航相关推荐

  1. 调用第三方地图app导航(高德、百度、腾讯)

    当前位置导航到某个地方,还可以设置出发地的经纬度和位置信息,具体看各官网参数设置. 以下代码可以直接使用. /*** Date: 2022-11-23* Author: lanzi* 调用第三方地图a ...

  2. IOS 调用第三方地图APP导航

    在开发中调用第三方APP进行路径规划,在此过程中不需要导入所调用地图的SDK,本文中只介绍调用百度地图.高德地图以及苹果自身地图APP. 在本项目中我用的是百度地图编码获取的百度经纬度坐标,而在开发过 ...

  3. Android仿微信调用第三方地图应用导航(高德、百度、腾讯)

    好久没有写Andorid代码啦!最近刚好要实现一个这个功能,顺便就在博客里分享一下. 实现目标 先来一张微信功能截图看看要做什么 其实就是有一个目的地,点击目的地的时候弹出可选择的应用进行导航. 大脑 ...

  4. Android仿高德地图app,Android仿微信调用第三方地图应用导航(高德、百度、腾讯)...

    好久没有写Andorid代码啦!最近刚好要实现一个这个功能,顺便就在博客里分享一下. 实现目标 先来一张微信功能截图看看要做什么 其实就是有一个目的地,点击目的地的时候弹出可选择的应用进行导航. 大脑 ...

  5. iOS在APP中调用第三方地图地图(苹果,高德,百度,腾讯)

    源码地址https://download.csdn.net/download/liuyinghui523/10801074 1.在app中获得定位权限 <key>NSLocationAlw ...

  6. Android 调用第三方地图进行导航

    前言 最近项目中需要实现定位导航到某个目的地去.看网上说有人集成了某些平台的sdk进行导航操作.其实可以不用集成sdk文档(只是针对要导航到xx地方去的业务需求,只需要知道当前位置和目的地的坐标即可. ...

  7. android app调用第三方地图路线规划导航(百度,高德,腾讯)

    android app调用第三方地图路线规划导航(百度,高德,腾讯) 因为直接使用高德的sdk提供的导航被投诉说不准,所以需要接第三方. 把BAT系的地图都接上了,有兄弟找到其他的地图调用方法告诉下小 ...

  8. 安卓调起高德百度第三方地图进行导航

    项目中用到了导航功能,本着简单的做法,就想到了直接调用高德百度第三方地图进行导航,这样可以减少包的体积,在网上百度了许久,各种都有,自己总结了一下,目前测试还算好用,拿出来晒一下,知道的就别喷了,我是 ...

  9. 基于android对接百度地图搜索附近关键字列表展示并调用第三方地图应用打开导航(百度地图、高德地图)

    经过两天的折腾,终于把需求实现了,记录下过程与遇到的坑(详情链接) 先上效果图 搜索结果 点击列表中的去这里调取第三方地图APP,效果图 首先配置百度地图 bBaiduMap = mMapView.g ...

最新文章

  1. C++调用Python
  2. python的6种基本数据类型--字典
  3. 分布式系统关注点(21)——构建「易测试」系统的“六脉神剑”
  4. 升级nodejs至最新
  5. ssm read time out的原因_为什么得肝病的男人越来越多?爱喝酒不是原因,或跟老婆有关系!...
  6. EUCM鱼眼相机模型详解
  7. 链表相关的面试题型总结
  8. 苹果cmsv10仿美剧吧自适应好看的免费高端简约模板
  9. Windows网络编程之UDP通信
  10. 【龙印】龙芯1c上双路16位AD芯片TM7705的linux驱动
  11. wordpress入门主题_WordPress儿童主题入门
  12. 计算机打字训练教学教案,打字练习小游戏教案.doc
  13. 四轴笔记----PSRAM存储器介绍
  14. Android Studio修改apk命名
  15. ArcGIS Server 自定义比例尺切图
  16. 【Grace卫星】Grace卫星精度知识点。
  17. 网络攻防原理与技术 第一章 课后题
  18. Xshell实现Windows上传文件到Linux主机的方法
  19. Revit建模中如何快速画好幕墙?
  20. 增加BUG的测试版本字段

热门文章

  1. PCA 主成分分析-清晰详细又易懂
  2. 微信小程序-全局倒计时+全局弹窗提示
  3. mysql单表数据列_MySQL 之 数据操作及单表查询
  4. 多功能全能计算机,全能计算机app
  5. 复杂字符串分列,例如将“花生100公斤”拆分到三个单元格,分别是花生、100、公斤
  6. 翻译: 给有野心的19岁少年的建议——Sam Altman
  7. !=会命中索引么_真是命中! 通过流量分析衡量成功
  8. ●线段树题之wows
  9. 石子合并 动态规划(直线型)
  10. 三星linux应用程序,三星推出Linux on DeX应用程序,正式发布高端手机新品W2019