swift5学习之旅之MapKit调用第三方地图

27 篇文章 0 订阅

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)
        }))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值