调用苹果地图、百度地图、高德地图导航(不需要集成sdk)

最近在研究地图,所以就简单写了一个小 demo,给大家分享一下如何不集成SDK就能调用第三方地图app,这里只介绍苹果自带地图、百度地图和高德地图的调用!其中还简单介绍了app定位功能,希望能给初学的开发者带来一点帮助!(swift)
import MapKit

class MapViewController: UIViewController,CLLocationManagerDelegate {

    /**  位置管理器  */
    var locationManager : CLLocationManager = CLLocationManager()
    var myLocation : CLLocationCoordinate2D!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()//询问是否获取当前位置
        openLocationService()
        // Do any additional setup after loading the view.
    }
    /**  CLLocationManagerDelegate  */
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currentLatitude = locations.last?.coordinate.latitude
        let currentLongitude = locations.last?.coordinate.longitude
        print(currentLatitude)
        print(currentLongitude)
        myLocation = locations.first?.coordinate
        //停止更新位置(如果定位服务不需要实时更新的话,那么应该停止位置的更新)
        locationManager.stopUpdatingLocation()
    }
    /**  目标位置  */
    func getGoalLocation() -> CLLocationCoordinate2D {
        return CLLocationCoordinate2DMake(22.648838,114.026178)
    }

    //苹果地图
    @IBAction func openAppleMapAction(sender: UIButton) {
        let currentLocationItem = MKMapItem.mapItemForCurrentLocation()
        let toLocationItem = MKMapItem.init(placemark: MKPlacemark.init(coordinate: getGoalLocation(), addressDictionary: nil))
        toLocationItem.name = "大浪"
        MKMapItem.openMapsWithItems([currentLocationItem,toLocationItem], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving , MKLaunchOptionsShowsTrafficKey: true])
    }
    //百度地图
    @IBAction func openBMKMapAction(sender: UIButton) {
        if application.canOpenURL(NSURL.init(string: "baidumap://map/")!) {
            let urlString = "baidumap://map/direction?origin=latlng:\(22.6199163368547),\(114.018015580424)|name:我的位置&destination=latlng:\(getGoalLocation().latitude),\(getGoalLocation().longitude)|name:大浪&mode=driving"
            ["name":"百度地图","url":urlString]
            application.openURL(NSURL.init(string: urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.init(charactersInString: "`#%^{}\"[]|\\<> ").invertedSet)!)!)
        }else{
            application.openURL(NSURL.init(string: "https://itunes.apple.com/cn/app/bai-du-tu-shou-ji-tu-lu-xian/id452186370?mt=8")!)
        }
    }
    //高德地图
    @IBAction func openAMapAction(sender: UIButton) {
        if application.canOpenURL(NSURL.init(string: "iosamap://")!) {
            let urlString = "iosamap://navi?sourceApplication=充电桩&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=\(getGoalLocation().latitude)&lon=\(getGoalLocation().longitude)&dev=0&style=3"
            ["name":"高德地图","url":urlString]
            application.openURL(NSURL.init(string: urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.init(charactersInString: "`#%^{}\"[]|\\<> ").invertedSet)!)!)
        }else{
            application.openURL(NSURL.init(string: "https://itunes.apple.com/cn/app/gao-tu-zhuan-ye-shou-ji-tu/id461703208?mt=8")!)
        }
    }

    /**  开启定位服务  */
    func openLocationService() {
        if CLLocationManager.locationServicesEnabled() {
            if CLLocationManager.authorizationStatus() == .AuthorizedAlways || CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
                self.locationManager.distanceFilter = kCLDistanceFilterNone//任何的移动都进行重新定位
                //设置定位的精准度,一般精准度越高,越耗电(这里设置为精准度最高的,适用于导航应用)
                self.locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation
                self.locationManager.startUpdatingLocation()
            }else{
                UIAlertController.showAlert(self, title: "定位服务已关闭", message: "请在“设置” > “隐私”中打开“定位服务”来允许“充电桩”使用您的当前位置", cancelButtonTitle: "好", okButtonTitle: "设置", okHandler: { (alert) in
                    application.openURL(NSURL.init(string: "prefs:root=LOCATION_SERVICES")!)
                })
            }
        }
    }

这样就已经完成了,就能调起百度地图了吗?不行,到这里我们还是不能调用,原因是在iOS9.0之后我们需要在info文件中配置一些东西,就像我们做分享的时候需要添加白名单一样
这里写图片描述这样我们就能调起百度地图地图完成导航了。。。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值