博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ios】导航跳转
阅读量:7092 次
发布时间:2019-06-28

本文共 5418 字,大约阅读时间需要 18 分钟。

hot3.png

public class MapNavigationKit: NSObject {        public enum MapType {        // 苹果        case apple        // 谷歌        case google        // 百度        case baidu        // 高德        case amap        // 腾讯地图        case tencent                var openURL: String {            switch self {            case .apple:                return "http://maps.apple.com/"            case .amap:                return "iosamap://"            case .baidu:                return "baidumap://"            case .google:                return "comgooglemaps://"            case .tencent:                return "qqmap://"            }        }                var appName: String {            switch self {            case .apple:                return NSLocalizedString("NAVIGATION_APPLE", comment: "苹果地图")            case .amap:                return NSLocalizedString("NAVIGATION_AMAP", comment: "高德地图")            case .baidu:                return NSLocalizedString("NAVIGATION_BAIDU", comment: "百度地图")            case .google:                return NSLocalizedString("NAVIGATION_GOOGLE", comment: "谷歌地图")            case .tencent:                return NSLocalizedString("NAVIGATION_TENCENT", comment: "腾讯地图")            }        }                var parameterFormat: String {            switch self {            case .apple:                return ""            case .amap:                return "iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2"            case .baidu:                return "baidumap://map/direction?origin={
{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02" case .google: return "comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving" case .tencent: return "qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&coord_type=1&policy=0" } } } public static let shared = MapNavigationKit() fileprivate var _mapTypes: [MapType] = [.apple, .google, .baidu, .amap, .tencent] fileprivate var _fromCoordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0) fileprivate var _toCoordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0) private override init() { super.init() } /* 弹出对话框 - parameter coordinate: 经纬度(GCJ) - parameter view: 当前的ViewController */ public func selectMapApp(fromCoordinate: CLLocationCoordinate2D, toCoordinate: CLLocationCoordinate2D, view: UIView) { _fromCoordinate = fromCoordinate _toCoordinate = toCoordinate let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: NSLocalizedString("SECURE_AREA_DELETE_CANCEL", comment: "取消"), destructiveButtonTitle: nil) _mapTypes.forEach { (type) in if UIApplication.shared.canOpenURL(URL(string: type.openURL)!) == true { actionSheet.addButton(withTitle: type.appName) } } actionSheet.show(in: view) }}extension MapNavigationKit: UIActionSheetDelegate { public func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) { guard let buttonTitle = actionSheet.buttonTitle(at: buttonIndex) else { return } let appName = "自己根据需求填写" let urlScheme = "自己根据需求填写" switch buttonTitle { case MapType.apple.appName: let currentLocation = MKMapItem.forCurrentLocation() let toLocation = MKMapItem(placemark: MKPlacemark(coordinate: _toCoordinate, addressDictionary: nil)) MKMapItem.openMaps(with: [currentLocation, toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: true]) case MapType.google.appName: guard let url = String(format: MapType.google.parameterFormat, appName, urlScheme, _toCoordinate.latitude, _toCoordinate.longitude).addingPercentEscapes(using: String.Encoding.utf8) else { return } UIApplication.shared.openURL(URL(string: url)!) case MapType.amap.appName: guard let url = String(format: MapType.amap.parameterFormat, appName, urlScheme, _toCoordinate.latitude, _toCoordinate.longitude).addingPercentEscapes(using: String.Encoding.utf8) else { return } UIApplication.shared.openURL(URL(string: url)!) case MapType.baidu.appName: guard let url = String(format: MapType.baidu.parameterFormat, _toCoordinate.latitude, _toCoordinate.longitude).addingPercentEscapes(using: String.Encoding.utf8) else { return } UIApplication.shared.openURL(URL(string: url)!) case MapType.tencent.appName: guard let url = String(format: MapType.tencent.parameterFormat, _fromCoordinate.latitude, _fromCoordinate.longitude, _toCoordinate.latitude, _toCoordinate.longitude).addingPercentEscapes(using: String.Encoding.utf8) else { return } UIApplication.shared.openURL(URL(string: url)!) default: return } } }

 

转载于:https://my.oschina.net/gejw0623/blog/752851

你可能感兴趣的文章
Java异常面试题
查看>>
JDBC对Mysql utf8mb4字符集的处理
查看>>
使用express-generator初始化你的项目目录
查看>>
使用JavaScript脚本控制媒体播放(顺序播放和随机播放)
查看>>
01背包
查看>>
poj1363 Rails
查看>>
ELK-Python(二)
查看>>
我的FP感悟
查看>>
linux 系统操作
查看>>
WebView使用总结(应用函数与JS函数互相调用)
查看>>
WuKong
查看>>
Quick Sort(Java)
查看>>
1029. 旧键盘(20)
查看>>
Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode ...
查看>>
Javascript做图片无缝平滑滚动
查看>>
spring mvc深入学习
查看>>
jquery中的跨域-jsonp格式
查看>>
【原】iOS学习之图片拉伸处理(类似qq的气泡)
查看>>
Oracle中修改某个字段可以为空
查看>>
[ USACO 2007 FEB ] Lilypad Pond (Gold)
查看>>