[Swift]地图导航
如图,先在白名单(LSApplicationQueriesSchemes)中添加需要支持的地图(comgooglemaps、qqmap、baidumap、iosamap).
使用时传起点位置、终点位置和目标名称.
/// 导航
@IBAction func tapNavMapAction(_ sender: Any) {MapNavigationHelper.shared.goToHere(fromCoordinate: fromCoordinate(), toCoordinate: toCoordinate(), destination: shopModel?.shopName ?? "店铺")
}func fromCoordinate() -> CLLocationCoordinate2D {let tempLatitude = AppUserDefaults.shared.latitude ?? ""let tempLongitude = AppUserDefaults.shared.longitude ?? ""let fromCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(CLLocationDegrees(tempLatitude.toFloat() ?? 0.0), CLLocationDegrees(tempLongitude.toFloat() ?? 0.0))return fromCoordinate
}func toCoordinate() -> CLLocationCoordinate2D {let tempLatitude = shopModel?.latitude ?? ""let tempLongitude = shopModel?.longitude ?? ""let toCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(CLLocationDegrees(tempLatitude.toFloat() ?? 0.0), CLLocationDegrees(tempLongitude.toFloat() ?? 0.0))return toCoordinate
}
地图导航助手
import Foundation
import MapKitclass MapNavigationHelper: NSObject {var fromCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D()var toCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D()var destination: String = "店铺"static let shared = MapNavigationHelper()/**@abstract 到这里去@param fromCoordinate:起点@param toCoordinate:终点@param destination:目的地*/func goToHere(fromCoordinate: CLLocationCoordinate2D, toCoordinate: CLLocationCoordinate2D, destination: String) {self.fromCoordinate = fromCoordinateself.toCoordinate = toCoordinateself.destination = destinationpopAlert()}/// 弹出提示func popAlert() {guard let topVC = ScreenUIManager.topViewController() else {return}var tempMsg = "导航到店铺的位置"let tempName = destinationif destination != "" {tempMsg = "导航到“\(tempName)”的位置"}let alert = UIAlertController(title: nil, message: tempMsg, preferredStyle: .actionSheet)if let tempURL = URL(string: "http://maps.apple.com/") , UIApplication.shared.canOpenURL(tempURL) {let appleMapAction = UIAlertAction(title: "苹果地图", style: .default) { [weak self] action inself?.doAppleNavigation()}alert.addAction(appleMapAction)}if let tempURL = URL(string: "baidumap://") , UIApplication.shared.canOpenURL(tempURL) {let baiduMapAction = UIAlertAction(title: "百度地图", style: .default) { [weak self] action inself?.doBaiduNavigation()}alert.addAction(baiduMapAction)}if let tempURL = URL(string: "iosamap://") , UIApplication.shared.canOpenURL(tempURL) {let gaodeMapAction = UIAlertAction(title: "高德地图", style: .default) { [weak self] action inself?.doGaodeNavigation()}alert.addAction(gaodeMapAction)}if let tempURL = URL(string: "qqmap://") , UIApplication.shared.canOpenURL(tempURL) {let gaodeMapAction = UIAlertAction(title: "腾讯地图", style: .default) { [weak self] action inself?.doQQNavigation()}alert.addAction(gaodeMapAction)}if let tempURL = URL(string: "comgooglemaps://") , UIApplication.shared.canOpenURL(tempURL) {let googleMapAction = UIAlertAction(title: "谷歌地图", style: .default) { [weak self] action inself?.doGoogleNavigation()}alert.addAction(googleMapAction)}let cancelAction = UIAlertAction(title: "取消", style: .cancel) { action in}alert.addAction(cancelAction)topVC.present(alert, animated: true, completion: nil)}/*** 苹果地图路线规划* 文档:https://developer.apple.com/documentation/mapkit/mkmapitem*/func doAppleNavigation() {let fromPlacemark: MKPlacemark = MKPlacemark(coordinate: fromCoordinate, addressDictionary: nil)let toPlacemark: MKPlacemark = MKPlacemark(coordinate: toCoordinate, addressDictionary: nil)let fromItem: MKMapItem = MKMapItem(placemark: fromPlacemark)fromItem.name = "我的位置"let toItem: MKMapItem = MKMapItem(placemark: toPlacemark)toItem.name = destinationMKMapItem.openMaps(with: [fromItem,toItem], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: NSNumber(value: true)])}/*** 百度地图路线规划* 文档:http://lbsyun.baidu.com/index.php?title=uri/api/ios*/func doBaiduNavigation() {var urlString = "baidumap://map/direction?origin={{我的位置}}&destination=name:\(destination)|latlng:\(toCoordinate.latitude),\(toCoordinate.longitude)&mode=driving&coord_type=gcj02"urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!guard let tempURL: URL = NSURL(string: urlString) as URL? else {return}if UIApplication.shared.canOpenURL(tempURL) {if #available(iOS 10.0, *) {UIApplication.shared.open(tempURL, options: [:], completionHandler: nil)} else {// Fallback on earlier versionsUIApplication.shared.openURL(tempURL)}}}/*** 高德地图路线规划* 文档:http://lbs.amap.com/api/amap-mobile/guide/ios/route*/func doGaodeNavigation() {let infoDict = Bundle.main.infoDictionarylet tempAppName = "\(String(describing: infoDict?["CFBundleDisplayName"]))"let tempUrlScheme = "demoURL://";var urlString = "iosamap://path?sourceApplication=\(tempAppName)&backScheme=\(tempUrlScheme)&sname=我的位置&dlat=\(toCoordinate.latitude)&dlon=\(toCoordinate.longitude)&dname=\(String(describing: destination))&dev=0&style=2&m=0&t=0"urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!guard let tempURL: URL = NSURL(string: urlString) as URL? else {return}if UIApplication.shared.canOpenURL(tempURL) {if #available(iOS 10.0, *) {UIApplication.shared.open(tempURL, options: [:], completionHandler: nil)} else {// Fallback on earlier versionsUIApplication.shared.openURL(tempURL)}}}/*** 腾讯地图路线规划* 文档:http://lbs.qq.com/uri_v1/guide-route.html*/func doQQNavigation() {let infoDict = Bundle.main.infoDictionarylet tempAppName = "\(String(describing: infoDict?["CFBundleDisplayName"]))"var urlString = "qqmap://map/routeplan?type=drive&from=我的位置&to=\(String(describing: destination))&tocoord=\(toCoordinate.latitude),\(toCoordinate.longitude)&coord_type=1&policy=0&refer=\(tempAppName)"urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!guard let tempURL: URL = NSURL(string: urlString) as URL? else {return}if UIApplication.shared.canOpenURL(tempURL) {if #available(iOS 10.0, *) {UIApplication.shared.open(tempURL, options: [:], completionHandler: nil)} else {// Fallback on earlier versionsUIApplication.shared.openURL(tempURL)}}}/*** 谷歌地图路线规划* 文档:https://developers.google.com/maps/documentation/ios/urlscheme*/func doGoogleNavigation() {let infoDict = Bundle.main.infoDictionarylet tempAppName = "\(String(describing: infoDict?["CFBundleDisplayName"]))"let tempUrlScheme = "demoURL://"var urlString = "comgooglemaps://?x-source=\(tempAppName)&x-success=\(tempUrlScheme)&saddr=&daddr=\(toCoordinate.latitude),\(toCoordinate.longitude)&directionsmode=driving"urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!guard let tempURL: URL = NSURL(string: urlString) as URL? else {return}if UIApplication.shared.canOpenURL(tempURL) {if #available(iOS 10.0, *) {UIApplication.shared.open(tempURL, options: [:], completionHandler: nil)} else {// Fallback on earlier versionsUIApplication.shared.openURL(tempURL)}}}}
效果图:
[Swift]地图导航相关推荐
- iOS Swift 应用内跳转第三方地图导航路线 及地图坐标系转换
支持百度 谷歌 高德 苹果 腾讯地图 一键打开及 地图之间的坐标系的转换 本项目 Demo 下载地址 https://github.com/sinorychan/JumpToThirdMap 注意 ...
- html调用百度地图语音播报,实现百度地图导航演示的语音播放功能
在上面,百度地图导航演示是本地导入的,那么如何在此基础上实现导航语音广播呢? 首先,为应用程序申请语音广播(也称为注册) SDK具有内置的百度TTS语音广播功能,在使用该应用程序之前,需要对应用程序进 ...
- Windows phone 8 学习笔记(8) 定位地图导航
Windows phone 8 学习笔记(8) 定位地图导航 原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控 ...
- 实现百度地图导航Demo的语音播报功能
上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块...
config.xml文件的配置如下: <widget left="10" top="50" config="widgets/Navigation ...
- 微信路况会不会超越地图导航?
近日,微信公众平台用户量最大的公众号"微信路况"与康盛"微社区"达成合作,成立国内首家路况主题微社区"路况社区".从目前了解的信息情况来看, ...
- 百度地图 key_Android百度地图导航的接入(包含驾车、公交、步行)
百度地图导航的接入(包含三种选择方式驾车.公交.步行) 步骤 1.下载百度的sdk(下载地址:http://lbsyun.baidu.com/sdk/download) 勾选下载"检索功 ...
- iOS 调用地图导航
在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevic ...
- 百度地图no result available_【整理之路二】百度地图的路径规划和调用本机地图导航...
推荐看完之后注意一下最后的东西 一.细说百度地图的路径规划 路径规划主要有这么几种 1.公交路径规划 1.1 市内公交规划(暂时不在这里说) 1.2 跨市/省公交规划 // 导入头文件 #import ...
最新文章
- oracle快速导出1t数据,使用sqluldr2快速高效大批量导出Oracle数据
- 贪吃蛇游戏(附源码)
- matlab m语言,我要编写matlab一个公式的m语言,请问错在哪里?,M语言的MATLAB的M语言...
- 状态机-重叠序列检测
- 从一般到特殊-C#中的对象
- Hadoop入门(十七)Mapreduce的多表关联程序
- ws2812b程序51单片机_51单片机串口通信程序详解
- 年度调薪一般涨多少_宁愿辞职也不敢提加薪,心理专家告诉你加薪提多少才合适...
- 线切割常用专用编程软件下载
- RestLet框架的入门
- 时间格式转化大汇总各种类型
- 小宝机器人怎么开机_小宝机器人用户手册.PDF
- 阿里2018届实习生内推经历
- 一款值得使用的会议室管理软件【会议预订小程序】
- HTTP协议-python接口自动化话测试(无涯)
- 批量合并word文档
- ORA-01779: 无法修改与非键值保存表对应的列
- uview Checkbox 复选框勾选,删除某一项遇见的bug,复用导致的问题
- 20.深度解密二十:通过搜索引擎进行淘宝客营销玩法
- 参与影片《大城小事》的记者会