声明:本文技术核心来自swift iOS9之后,打开第三方地图导航,但由于版本升级,部分代码已经不能用了,所以才写了此博文,方便自查
1、建立新项目
2 、在项目属性TARGETS->Bulid Phases的Link Binary With Libraries中添加MapKit.framework
3、在info.plist添加LSApplicationQueriesSchemes,类型为Array,然后添加四个item,分别是:baidumap,iosamap,comgooglemaps,qqmap
4、创建全局变量
var optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
5、添加方法:
//参数是我自己加的,目的地的经纬度和标题,
func creatOptionMenu(lat: String,lng: String,title:String){
let SHARE_APPLICATION = UIApplication.shared
if(SHARE_APPLICATION.canOpenURL(NSURL(string:"qqmap://")! as URL) == true){
let qqAction = UIAlertAction(title: "腾讯地图", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let urlString = "qqmap://map/routeplan?from=我的位置&type=drive&tocoord=\(lat),\(lng)&to=\(title)&coord_type=1&policy=0"
//let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
//let url = URL(string: urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()))
let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
SHARE_APPLICATION.openURL(url!)
})
optionMenu.addAction(qqAction)
}
if(SHARE_APPLICATION.canOpenURL(NSURL(string:"iosamap://")! as URL) == true){
let gaodeAction = UIAlertAction(title: "高德地图", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let urlString = "iosamap://navi?sourceApplication=app名&backScheme=iosamap://&lat=\(lat)&lon=\(lng)&dev=0&style=2"
//let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
SHARE_APPLICATION.openURL(url!)
})
optionMenu.addAction(gaodeAction)
}
if(SHARE_APPLICATION.canOpenURL(NSURL(string:"comgooglemaps://")! as URL) == true){
let googleAction = UIAlertAction(title: "Google地图", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let urlString = "comgooglemaps://?x-source=app名&x-success=comgooglemaps://&saddr=&daddr=\(lat),\(lng)&directionsmode=driving"
//let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
SHARE_APPLICATION.openURL(url!)
})
optionMenu.addAction(googleAction)
}
let appleAction = UIAlertAction(title: "苹果地图", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let loc = CLLocationCoordinate2DMake(Double(lat)!, Double(lng)!)
let currentLocation = MKMapItem.forCurrentLocation()
let toLocation = MKMapItem(placemark:MKPlacemark(coordinate:loc,addressDictionary:nil))
toLocation.name = title
MKMapItem.openMaps(with: [currentLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: NSNumber(value: true)])
})
optionMenu.addAction(appleAction)
if(SHARE_APPLICATION.canOpenURL(NSURL(string:"baidumap://")! as URL) == true){
let baiduAction = UIAlertAction(title: "百度地图", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let urlString = "baidumap://map/direction?origin={{我的位置}}&destination=latlng:\(lat),\(lng)|name=\(title)&mode=driving&coord_type=gcj02"
//let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(String.Encoding.utf8)!)
let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)
SHARE_APPLICATION.openURL(url!)
})
optionMenu.addAction(baiduAction)
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
})
optionMenu.addAction(cancelAction)
}
6、调用
在需要执行的方法体里调用以下代码:
creatOptionMenu(lat: latstr,lng: lngstr,title: titstr)
self.present(optionMenu, animated: true, completion: nil)
//要重置optionMenu
self.optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
如果在同一view需要多次调用,在调用之后需要重置optionMenu,否则会崩溃