swift

1.

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件

  

2.

import Foundation

typealias POIResultBlock = ([BMKPoiInfo])->()

class BaiduTool: NSObject {

    static let shareInstance = BaiduTool()
    
    
    private lazy var searcher: BMKPoiSearch = {
        
        let searcher = BMKPoiSearch()
        searcher.delegate = self
        return searcher
        
    }()
    
    private var poiResultBlock: POIResultBlock?
    
    
    func beginNav(startCoordinate: CLLocationCoordinate2D, endCoordinate: CLLocationCoordinate2D) {
        //节点数组
        var nodesArray = [BNRoutePlanNode]()
        //起点
        let startNode = BNRoutePlanNode()
        startNode.pos = BNPosition()
        startNode.pos.x = startCoordinate.longitude;
        startNode.pos.y = startCoordinate.latitude;
        startNode.pos.eType = BNCoordinate_BaiduMapSDK;
        nodesArray.append(startNode)
        
        //终点
        let endNode = BNRoutePlanNode()
        endNode.pos = BNPosition()
        endNode.pos.x = endCoordinate.longitude
        endNode.pos.y = endCoordinate.latitude
        endNode.pos.eType = BNCoordinate_BaiduMapSDK;
        nodesArray.append(endNode)
        
        BNCoreServices.RoutePlanService().startNaviRoutePlan(BNRoutePlanMode_Recommend, naviNodes: nodesArray, time: nil, delegete: self  , userInfo: nil)
    }
    
    
    class func addAnnotation(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, toMapView: BMKMapView) {
//        添加一个PointAnnotation
        let annotation = BMKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = title
        annotation.subtitle = subtitle
        
        toMapView.addAnnotation(annotation)

    }
    
    
    func getPOIInfos(keyWord: String, center: CLLocationCoordinate2D, result: POIResultBlock?) {
        
        // 记录代码块, 在合适的地方执行
        poiResultBlock = result
        
        
        // poi检索
        
        //发起检索
        let option = BMKNearbySearchOption()
        // 起始页码
        option.pageIndex = 0
        // 每一页的容量
        option.pageCapacity = 20
        // 周边检索(谁的周边,中心店)
        // CLLocationCoordinate2DMake(39.915, 116.404)
        option.location = center
        option.keyword = keyWord
        
        let flag = searcher.poiSearchNearBy(option)
        
        if flag
        {
            print("周边检索发送成功")
        }
        else
        {
            print("周边检索发送失败")
        }
        

        
    }
    
}



extension BaiduTool: 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]
            
            if poiResultBlock != nil {
                poiResultBlock!(poiInfos)
            }
       
            
        }else {
            print("检索失败")
        }
        
    }

    
}


extension BaiduTool: BNNaviRoutePlanDelegate {
    
    func routePlanDidFinished(userInfo: [NSObject : AnyObject]!) {
        BNCoreServices.UIService().showNaviUI(BN_NaviTypeSimulator, delegete: nil , isNeedLandscape: false)
    }
    
    
    
    
}

  

原文地址:https://www.cnblogs.com/qingzZ/p/10195823.html