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
}
}
}