swift地图定位(二十一)集成百度导航SDK

//
//  ViewController.swift
//  baidumapfirsttest
//
//  Created by targetcloud on 2016/11/16.
//  Copyright © 2016年 targetcloud. All rights reserved.

//集成百度导航
/*
准备:
 1、下载包
 2、配置AK
 
集成:
 1、拖包 (直接拖入/Users/targetcloud/Desktop/BaiduNaviSDK_v3.0.0/BaiduNaviSDK 这件夹到工程即可)
 
 2、配置依赖的系统库(导入过的不需要再次添加)
 AudioToolbox.framework
 ImageIO.framework
 CoreMotion.framework
 CoreLocation.framework
 CoreTelephony.framework
 MediaPlayer.framework
 AVFoundation.framework
 SystemConfiguration.framework
 JavaScriptCore.framework
 Security.framework
 OpenGLES.framework
 GLKit.framework
 libstdc++6.0.9.tbd
 libsqlite3.0.tbd
 libz.1.2.5.tbd
 
 3、配置info.plist
 Required background modes
  >App plays audio or streams audio/video using AirPlay
  >App registers for location updates
 Privacy - Location Always Usage Description
  >只有开启定位功能才能正常使用百度导航
 Privacy - Location When In Use Usage Description
  >只有开启定位功能才能正常使用百度导航
 View controller-based status bar appearance
  >NO
 LSApplicationQueriesSchemes
  >baidumap
 
 4、桥接文件中补充一句(没有桥接文件参考集成百度地图SDK博文)
    #import "BNCoreServices.h"
 
 5、在func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {添加
 // 导航授权
 BNCoreServices.getInstance().initServices("wRRSuxGeEiD1yiPv8ej4XucCnTbjtQfR")
 BNCoreServices.getInstance().startAsyn({ () -> Void in
 print("success")
 }) { () -> Void in
 print("fail")
 }
 
 6、TTS设置 $(TARGET_NAME)
 http://app.navi.baidu.com/ttsregister/appinfo
 
 7、添加导航代码
*/
import UIKit

class ViewController: UIViewController {

    var _mapView: BMKMapView?
    
    lazy var searcher: BMKPoiSearch = {
        let searcher = BMKPoiSearch()
        searcher.delegate = self
        return searcher
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        _mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        self.view.addSubview(_mapView!)
        //设置起始中心点
        let center = CLLocationCoordinate2D(latitude: 31.272881101147327, longitude: 121.61539365113157)
        _mapView?.centerCoordinate = center
        //设置区域
        let span = BMKCoordinateSpanMake(0.011929035022411938, 0.0078062748817018246)
        let region = BMKCoordinateRegionMake(center, span)
        _mapView?.setRegion(region, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        _mapView?.viewWillAppear()
        searcher.delegate = self
        _mapView?.delegate = self // 此处记得不用的时候需要置nil,否则影响内存的释放
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        _mapView?.viewWillDisappear()
        searcher.delegate = nil
        _mapView?.delegate = nil // 不用时,置nil
    }
}

extension ViewController: BMKMapViewDelegate {
    func mapview(_ mapView: BMKMapView!, onLongClick coordinate: CLLocationCoordinate2D) {
        print(coordinate);//CLLocationCoordinate2D(latitude: 31.273070191233703, longitude: 121.61624006488663)
        // 调整区域 根据长按的位置
        let span = BMKCoordinateSpanMake(0.011929035022411938, 0.0078062748817018246)
        let region = BMKCoordinateRegionMake(coordinate, span)
        mapView.setRegion(region, animated: true)
        
        let option = BMKNearbySearchOption()
        option.pageIndex = 0
        option.pageCapacity = 20
        option.location = coordinate//根据长按的位置
        option.keyword = "小吃"
        let flag = searcher.poiSearchNear(by: option)
        if flag{
            print("周边检索发送成功")
        }else{
            print("周边检索发送失败")
        }
    }
    
    //点击大头针
    func mapView(_ mapView: BMKMapView!, annotationViewForBubble view: BMKAnnotationView!) {
        let annotaiton = view.annotation
        print(">>>\(annotaiton?.coordinate)\(annotaiton?.title!())\(annotaiton?.subtitle!())")//>>>Optional(__C.CLLocationCoordinate2D(latitude: 31.273627860569221, longitude: 121.61636597701556))Optional("美滋滋零食")Optional("上海市浦东新区")
        
        //节点数组
        var nodesArray = [BNRoutePlanNode]()
        //起点
        let startNode = BNRoutePlanNode()
        startNode.pos = BNPosition()
        startNode.pos.x = 121.61539365113157;
        startNode.pos.y = 31.272881101147327;
        startNode.pos.eType = BNCoordinate_BaiduMapSDK;
        nodesArray.append(startNode)
        //终点
        let endNode = BNRoutePlanNode()
        endNode.pos = BNPosition()
        endNode.pos.x = (annotaiton?.coordinate.longitude)!
        endNode.pos.y = (annotaiton?.coordinate.latitude)!
        endNode.pos.eType = BNCoordinate_BaiduMapSDK;
        nodesArray.append(endNode)
        //发起路径规划
        BNCoreServices.routePlanService().startNaviRoutePlan(BNRoutePlanMode_Recommend, naviNodes: nodesArray, time: nil, delegete: self, userInfo: nil)
    }
    
    //区域改变
    func mapView(_ mapView: BMKMapView!, regionDidChangeAnimated animated: Bool) {
        print(mapView.region.center,mapView.region.span)//CLLocationCoordinate2D(latitude: 31.272873385065104, longitude: 121.61537568502136) BMKCoordinateSpan(latitudeDelta: 0.011921347023825746, longitudeDelta: 0.0078062748817018246)
    }
}

extension ViewController: BNNaviRoutePlanDelegate {
    func routePlanDidFinished(_ userInfo: [AnyHashable: Any]!) {
        BNCoreServices.uiService().showPage(BNaviUI_NormalNavi, delegate: nil, extParams: nil)
    }
}

extension ViewController: BMKPoiSearchDelegate {
    func onGetPoiResult(_ searcher: BMKPoiSearch!, result poiResult: BMKPoiResult!, errorCode: BMKSearchErrorCode) {
        if errorCode == BMK_SEARCH_NO_ERROR {
            print("周边检索成功")
            print(poiResult.poiInfoList)
            let poiInfos = poiResult.poiInfoList as! [BMKPoiInfo]
            for poiInfo in poiInfos {
                print(poiInfo.name, poiInfo.address)
                //加大头针
                let annotation = BMKPointAnnotation()
                annotation.coordinate = poiInfo.pt//CLLocationCoordinate2D
                annotation.title = poiInfo.name
                annotation.subtitle = poiInfo.address
                _mapView?.addAnnotation(annotation)
            }
        }else {
            print("周边检索失败")
        }
    }
}
真机运行OK ,模拟器运行会有TTS失败问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值