ios 百度地图的集成与使用

593 阅读2分钟

先附上百度地图官网

cocopods集成

pod 'BaiduMapKit' #百度地图SDK

百度地图头文件

#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> //只引入所需的单个头文件

AppDelegate配置

//声明
{
    UINavigationController *navigationController;
    BMKMapManager* _mapManager;
}

    //注册
    _mapManager = [[BMKMapManager alloc]init];
    // 百度地图key
    BOOL ret = [_mapManager start:@"您的Key"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

经纬度

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //经纬度
    NSLog(@"%f",userLocation.location.coordinate.latitude);
    NSLog(@"%f",userLocation.location.coordinate.longitude);
    //关闭坐标更新
    [self.locService stopUserLocationService];
}

地图BMKMapView

    //初始化地图
    self.mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.delegate = self;
    //设置地图的显示样式
    self.mapView.mapType = BMKMapTypeStandard;
    //底图poi标注
     self.mapView.showMapPoi = NO;
    //在手机上当前可使用的级别为3-21级
    self.mapView.zoomLevel = 21;
    //旋转
    self.mapView.rotateEnabled = NO;
    //拖拽
    self.mapView.scrollEnabled = NO;
    [self.view addSubview:self.mapView];
    //初始化定位
    self.service = [[BMKLocationService alloc] init];
    //设置代理
    self.service.delegate = self;
    //开启定位
    [self.service startUserLocationService];

#pragma mark BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _mapView.showsUserLocation = YES;//显示定位图层
    //设置地图中心为用户经纬度
    [_mapView updateLocationData:userLocation];
    CLLocationCoordinate2D coordinate = userLocation.location.coordinate;//位置坐标
    
    _mapView.centerCoordinate = userLocation.location.coordinate;//回到中心点
    BMKCoordinateRegion region ;//表示范围的结构体
    region.center = _mapView.centerCoordinate;//中心点
    region.span.latitudeDelta = 0.004;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
    region.span.longitudeDelta = 0.004;//纬度范围
    [_mapView setRegion:region animated:YES];
    
}

获取周边信息

#pragma mark BMKGeoCodeSearchDelegate
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    //获取周边用户信息
    if (error==BMK_SEARCH_NO_ERROR)
   {
        [self.cityDataArr removeAllObjects];
        for(BMKPoiInfo *poiInfo in result.poiList)
        {
            cityModel *model=[[cityModel alloc]init];
            model.name=poiInfo.name;
            model.address=poiInfo.address;
            model.pt=poiInfo.pt;
            [self.cityDataArr addObject:model];
            [self.cityTableview reloadData];
        }
    }else
    {
        NSLog(@"BMKSearchErrorCode: %u",error);
    }
    [_locService stopUserLocationService];
} 

POI检索

    _poiSearch = [[BMKPoiSearch alloc]init];
    _poiSearch.delegate = self;

    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
    citySearchOption.pageIndex = 0;
    citySearchOption.pageCapacity = 30;
    citySearchOption.city = @"重庆市";
    citySearchOption.keyword = @"小吃";
    BOOL flag = [_poiSearch poiSearchInCity:citySearchOption];
    if(flag) {
        NSLog(@"城市内检索发送成功");
    } else {
        NSLog(@"城市内检索发送失败");
    }

#pragma mark ---------实现BMKPoiSearchDelegate处理回调结果
-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode{
    if(errorCode == BMK_SEARCH_NO_ERROR) {
        self.addressArray = [NSMutableArray array];
        self.poiResultArr = [NSMutableArray array];
        [self.addressArray removeAllObjects];
        
        for (BMKPoiInfo *info in poiResult.poiInfoList) {
            cityModel *model = [[cityModel alloc] init];
            model.name = info.name;
            model.address = info.address;
            model.pt = info.pt;
            [self.addressArray addObject:model];
        }
        [self.addressTableView reloadData];
//        self.poiResultArr = poiResult.poiInfoList;//公交检索的uid
    } else {
        NSLog(@"抱歉,未找到结果");
    }
    NSLog(@"self.addressArray === %@",_addressArray);
}

Sug检索(地点输入提示检索)

    _searcher =[[BMKSuggestionSearch alloc]init];
    _searcher.delegate = self;

    BMKSuggestionSearchOption *option = [[BMKSuggestionSearchOption alloc] init];
    option.cityname = @"重庆市";
    option.keyword  = @"观音桥";
    BOOL flag = [_searcher suggestionSearch:option];
    if (flag) {
        NSLog(@"Sug检索发送成功");
    } else {
        NSLog(@"Sug检索发送失败");
    }

#pragma mark ---------实现BMKSuggestionSearchDelegate处理回调结果
- (void)onGetSuggestionResult:(BMKSuggestionSearch*)searcher result:(BMKSuggestionResult*)result errorCode:(BMKSearchErrorCode)error{
    if(error == BMK_SEARCH_NO_ERROR) {
        self.addressArray = [NSMutableArray array];
        [self.addressArray removeAllObjects];
        for (BMKPoiInfo *info in result.keyList) {
            cityModel *model = [[cityModel alloc] init];
            model.name = info;
            [self.addressArray addObject:model];
        }
        [self.addressTableView reloadData];
    } else {
        NSLog(@"抱歉,未找到结果");
    }
    NSLog(@"self.addressArray === %@",_addressArray);
}

公交信息检索

    _bussearcher = [[BMKBusLineSearch alloc]init];
    _bussearcher.delegate = self;
    //发起检索
    BMKBusLineSearchOption *buslineSearchOption = [[BMKBusLineSearchOption alloc]init];
    buslineSearchOption.city= @"重庆市";
    buslineSearchOption.busLineUid= @"uid";
    BOOL flag = [_bussearcher busLineSearch:buslineSearchOption];
    if(flag) {
        NSLog(@"busline检索发送成功");
    } else {
        NSLog(@"busline检索");
    }

//实现PoiSearchDeleage处理回调结果
- (void)onGetBusDetailResult:(BMKBusLineSearch*)searcher result:(BMKBusLineResult*)busLineResult errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
    } else {
        NSLog(@"抱歉,未找到结果");
    }
}