代码是swift 3.0,项目中传入的坐标是百度坐标,其中系统自带,高德,谷歌均使用高德地图sdk转换后的高德坐标,腾讯地图使用的是腾讯的坐标转换api进行坐标转换之后的坐标,腾讯坐标转换api使用需要去腾讯开放平台注册获得appkey,其他地图均无需注册获得appkey。
func selectRouteSoft(coordinate: CLLocationCoordinate2D, address: String) {
let actionSheet = UIAlertController.init(title: "导航到设备", message: nil, preferredStyle: .actionSheet)
let cancel = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
actionSheet.addAction(cancel)
// 系统地图导航
let nativemap = UIAlertAction.init(title: "系统自带", style: .default) { (action) in
let nativeCoor = AMapCoordinateConvert(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude), .baidu)
let fromLocation = MKMapItem.forCurrentLocation()
let toLocation = MKMapItem.init(placemark: MKPlacemark.init(coordinate: CLLocationCoordinate2DMake(nativeCoor.latitude, nativeCoor.longitude), addressDictionary: [kABPersonAddressStreetKey as String: address]))
MKMapItem.openMaps(with: [fromLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: true])
}
actionSheet.addAction(nativemap)
// 百度地图导航
// 集成文档地址:http://lbsyun.baidu.com/index.php?title=uri/api/ios
if UIApplication.shared.canOpenURL(URL.init(string: "baidumap://")!) {
let baidumap = UIAlertAction.init(title: "百度地图", style: .default) { (action) in
let modeBaidu = "driving"
let url:String = "baidumap://map/direction?origin={{我的位置}}&destination=name:"+address+"|latlng:\(coordinate.latitude),\(coordinate.longitude)&mode="+modeBaidu+"&coord_type=bd09ll"
UIApplication.shared.openURL(URL.init(string: url.addingPercentEncoding(withAllowedCharacters: CharacterSet.init(charactersIn: "#%^{}\"[]|\\<>").inverted)!)!)
}
actionSheet.addAction(baidumap)
}
// 高德地图导航
// 集成文档地址:http://lbs.amap.com/api/amap-mobile/guide/ios/route
if UIApplication.shared.canOpenURL(URL.init(string: "iosamap://")!) {
let iosamap = UIAlertAction.init(title: "高德地图", style: .default) { (action) in
let amapCoor = AMapCoordinateConvert(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude), .baidu)
let url: String = "iosamap://path?sourceApplication=应用名称&sid=BGVIS1&slat=&slon=&sname=&did=BGVIS2&dlat=\(amapCoor.latitude)&dlon=\(amapCoor.longitude)&dname="+address+"&dev=0&t=0"
UIApplication.shared.openURL(URL.init(string: url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)!)!)
}
actionSheet.addAction(iosamap)
}
// 腾讯地图导航
/*
* type 路线规划方式:公交 bus,驾车 drive,步行,walk
* from 起点名称,我的位置为当前位置
* to 终点名称
* tocoord 终点坐标
* coord_type 坐标类型:1 GPS,2 腾讯你坐标
* policy 公交:type=bus,policy有以下取值,policy的取值缺省为0
0:较快捷
1:少换乘
2:少步行
3:不坐地铁
驾车:type=drive,policy有以下取值
0:较快捷
1:无高速
2:距离
* referer 自己的app名称
*/
if UIApplication.shared.canOpenURL(URL.init(string: "sosomap://")!) {
let sosomap = UIAlertAction.init(title: "腾讯地图", style: .default) { (action) in
// 进行腾讯地图坐标转换
var params : [String: String] = [:]
params["locations"] = "\(coordinate.latitude),\(coordinate.longitude)" // 单个转换坐标(支持批量转换,每组用;分割即可)
params["type"] = "3" // 转换坐标为百度坐标
params["key"] = soso_appKey // 腾讯地图appKey
RMAlamofireManager.sharedInstance.convertToTecentCoord(params: params, view: (self.controller?.view)!, result: { (data, error) in
if error == nil {
if data?.dictionaryObject != nil {
if (data?.dictionaryObject?["status"] as! NSNumber).intValue == 0 {
let locations = data?.dictionaryObject?["locations"] as! [[String: NSNumber]]
let lat = (locations.first?["lat"]?.floatValue)!
let lon = (locations.first?["lng"]?.floatValue)!
// 使用sosomap无法导航成功,总是提示跨城市导航不支持
let url: String = "qqmap://map/routeplan?type=drive&from=我的位置&to="+address+"&tocoord=\(lat),\(lon)&referer=应用名称&coord_type=2&policy=0"
UIApplication.shared.openURL(URL.init(string: url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)!)!)
}
}
}
})
}
actionSheet.addAction(sosomap)
}
// 谷歌地图导航
// 集成文档地址:https://developers.google.com/maps/documentation/ios-sdk/urlscheme
/*
* saddr:设置路线搜索的起点。 它可以是一个纬度、经度或查询格式的地址。 如果它是返回多个结果的查询字符串, 将选择第一个结果。 如果该值留空,那么将使用该用户的当前位置。
* daddr:设置路线搜索的终点。 具有与 saddr 相同的格式和行为。
* directionsmode:交通方式。 可以设置为:driving、transit、bicycling 或 walking。
*/
if UIApplication.shared.canOpenURL(URL.init(string: "comgooglemaps://")!) {
let amapCoor = AMapCoordinateConvert(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude), .baidu)
let comgooglemaps = UIAlertAction.init(title: "谷歌地图", style: .default) { (action) in
let url: String = "comgooglemaps://?saddr=&daddr=\(amapCoor.latitude),\(amapCoor.longitude)&directionsmode=driving"
UIApplication.shared.openURL(URL.init(string: url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)!)!)
}
actionSheet.addAction(comgooglemaps)
}
self.controller?.present(actionSheet, animated: true, completion: nil)
}