//
// 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失败问题。
swift地图定位(二十一)集成百度导航SDK
最新推荐文章于 2023-05-06 06:30:21 发布