【ios】导航跳转



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值