标签:
//懒加载
private lazy var mapManager :BMKMapManager = {
return BMKMapManager()
}()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//如果需要关注网络及授权验证事件,请设定generalDelegate参数
let ret = mapManager.start("1Yl6AGhTBKPupy4fM6pTHGO6sFIoTtzi", generalDelegate: nil)
if ret == false {
print("manager start failed!")
}
return true
}
class ViewController: UIViewController {
//展示地图的view
@IBOutlet weak var mapView: BMKMapView!
//懒加载
private lazy var searcher : BMKPoiSearch = {
//初始化检索对象
let searcher = BMKPoiSearch()
//设置代理
searcher.delegate = self
return searcher
}()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
mapView.delegate = self
}
override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//将代理设置为nil,以免占用内存
mapView.delegate = nil
}
}
///MARK: - 实现代理方法(当用户长按的时候回调用)
extension ViewController : BMKMapViewDelegate {
func mapview(mapView: BMKMapView!, onLongClick coordinate: CLLocationCoordinate2D) {
//发起检索
let option = BMKNearbySearchOption()
//检索的第几页
option.pageIndex = 0
//每页检索的数量
option.pageCapacity = 20
//检索的区域
option.location = CLLocationCoordinate2DMake(39.915, 116.404)
//检索的关键字
option.keyword = "电影"
//开始检索
let flag = searcher.poiSearchNearBy(option)
if flag {
print("周边检索发送成功")
}else {
print("周边检索发送失败")
}
}
}
extension ViewController : BMKPoiSearchDelegate {
func onGetPoiResult(searcher: BMKPoiSearch!, result poiResult: BMKPoiResult!, errorCode: BMKSearchErrorCode) {
if errorCode == BMK_SEARCH_NO_ERROR {
//处理正常结果
print("获取到数据")
}else if errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD {
print("起始点有歧义")
}else {
print("抱歉,未找到结果")
}
}
}
//点弹框时调用该方法
func mapView(mapView: BMKMapView!, annotationViewForBubble view: BMKAnnotationView!) {
//获取当前大头针的坐标
let coodinate = view.annotation.coordinate
//节点数组
var nodesArray = [BNRoutePlanNode]()
//起点
let startNode = BNRoutePlanNode()
startNode.pos = BNPosition()
startNode.pos.x = coodinate.longitude
startNode.pos.y = coodinate.latitude + 0.1
startNode.pos.eType = BNCoordinate_BaiduMapSDK
nodesArray.append(startNode)
//终点
let endNode = BNRoutePlanNode()
endNode.pos = BNPosition()
endNode.pos.x = coodinate.longitude
endNode.pos.y = coodinate.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.span)
}
}
extension ViewController : BNNaviRoutePlanDelegate {
func routePlanDidFinished(userInfo: [NSObject : AnyObject]!) {
BNCoreServices.UIService().showNaviUI(BN_NaviTypeSimulator, delegete: nil, isNeedLandscape: true)
}
}
extension ViewController : BMKPoiSearchDelegate {
func onGetPoiResult(searcher: BMKPoiSearch!, result poiResult: BMKPoiResult!, errorCode: BMKSearchErrorCode) {
if errorCode == BMK_SEARCH_NO_ERROR {
//处理正常结果
let poiInfos = poiResult.poiInfoList as! [BMKPoiInfo]
//遍历
for poiInfo in poiInfos {
//添加一个PointAnnotation
let annotation = BMKPointAnnotation()
annotation.coordinate = poiInfo.pt
annotation.title = poiInfo.name
annotation.subtitle = poiInfo.address
//添加大头针
mapView.addAnnotation(annotation)
}
}else if errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD {
print("起始点有歧义")
}else {
print("抱歉,未找到结果")
}
}
}
标签:
原文地址:http://blog.csdn.net/xf931456371/article/details/51379816