swift5学习之旅------MapKit调用第三方地图
工程文件
Github还没上传,先用着Dropbox(可能要翻墙),看完有收获的感谢点个赞👍,如果翻不了墙要代码可以私聊我
https://www.dropbox.com/sh/9h0icjqbnormdat/AABW0L0Fy0ah7iPafAJ8jUuYa?dl=0
地图Dropbox
- 调用地图的代码
//自带地图
func appleMap(lat:Double,lng:Double,destination:String) {
let loc = CLLocationCoordinate2DMake(lat, lng)
let currentLocation = MKMapItem.forCurrentLocation()
let toLocation = MKMapItem(placemark:MKPlacemark(coordinate:loc,addressDictionary:nil))
toLocation.name = destination
let boo = MKMapItem.openMaps(with: [currentLocation,toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: NSNumber(value: true)])
print(boo)
}
// 打开高德地图
func amap(dlat:Double,dlon:Double,dname:String,way:Int) {
let appName = "Your appName"
let urlString = "iosamap://path?sourceApplication=\(appName)&dname=\(dname)&dlat=\(dlat)&dlon=\(dlon)&t=\(way)" as String
if self.openMap(urlString) == false {
print("您还没有安装高德地图")
let urlString = "itms-apps://itunes.apple.com/app/id452186370"
self.openURL(urlString: urlString)
}
}
// 打开腾讯地图
func qqmap(endAddress:String,way:String,lat:Double,lng:Double) {
let urlString = "qqmap://map/routeplan?type=\(way)&from=&fromcoord=CurrentLocation&to=\(endAddress)&tocoord=\(lat),\(lng)&referer=腾讯需要申请APPkey"
let str = urlString as String
if self.openMap(str) == false {
print("您还没有安装腾讯地图")
let urlString = "itms-apps://itunes.apple.com/app/id481623196"
self.openURL(urlString: urlString)
}
}
// 打开百度地图
func baidumap(endAddress:String,way:String,lat:Double,lng:Double) {
let coordinate = CLLocationCoordinate2DMake(lat, lng)
// 将高德的经纬度转为百度的经纬度
let baiduCoordinate = getBaiDuCoordinateByGaoDeCoordinate(coordinate: coordinate)
let destination = "\(baiduCoordinate.latitude),\(baiduCoordinate.longitude)"
let urlString = "baidumap://map/direction?" + "&destination=" + endAddress + "&mode=" + way + "&destination=" + destination
let str = urlString as String
if self.openMap(str) == false {
print("您还没有安装百度地图")
let urlString = "itms-apps://itunes.apple.com/app/id452186370" as String
self.openURL(urlString: urlString)
}
}
private func openMap(_ urlString: String) -> Bool {
let urlstr = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
guard let url = URL(string:urlstr) else {
return false
}
if UIApplication.shared.canOpenURL(url) == true {
self.openURL(urlString: urlString as String)
return true
} else {
return false
}
}
func openURL(urlString:String) {
let urlstr = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
guard let url = URL(string:urlstr) else {
return
}
//根据iOS系统版本,分别处理
if #available(iOS 10, *) {
UIApplication.shared.open(url , options: [:],
completionHandler: { (success) in
print(success)
})
} else {
UIApplication.shared.openURL(url )
}
}
// 高德经纬度转为百度地图经纬度
// 百度经纬度转为高德经纬度,减掉相应的值就可以了。
func getBaiDuCoordinateByGaoDeCoordinate(coordinate:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065)
}
AlertController的调用地图
alertController.addAction(UIAlertAction(title: "Apple 地图", style: .default, handler: { _ in
self.appleMap(lat:self.latitute , lng:self.longitute, destination: self.coortitle)
}))