iOS开发之百度地图的集成——地图标注&POI检索

本篇分为两部分:

一、地图标注

  第一步:首先创建 BMKMapView 视图

  第二步:在视图完全显示出来后设置,并实现代理方法

  第三步:运行程序,此时大头针效果可以正常显示

二、POI检索

  第一步:延时加载对象

  第二步:实现BMKPoiSearchDelegate代理方法

  第三步:实现 BMKPoiSearchDelegate 处理回调结果

  第四步:运行程序,此时便可检索到附近小吃相关标注


一、地图标注

标注BMKAnnotation一定要实现为标注对应的protocal<BMKMapViewDelegate>

第一步:首先创建 BMKMapView 视图

- (BMKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
        self.view = _mapView;
    }
    return _mapView;
}

 第二步:在视图完全显示出来后设置,并实现代理方法

- (void) viewDidAppear:(BOOL)animated {
    // 添加一个PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"这里是北京";
    annotation.subtitle = @"我为你无法呼吸~";
    [_mapView addAnnotation:annotation];
}

// 自定义添加大头针方法
- (void)addAnnoWithPT:(CLLocationCoordinate2D)coor andTitle:(NSString *)title andAddress:(NSString *)address {
    // 添加一个PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    annotation.coordinate = coor;
    annotation.title = title;
    annotation.subtitle = address;
    [_mapView addAnnotation:annotation];
}
#pragma mark
#pragma mark - BMKLocationServiceDelegate 代理方法,用于添加大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation {
    static NSString *identifier = @"myAnnotation";
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (!newAnnotationView) {
            newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        }
        newAnnotationView.annotation = annotation;
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        
        //添加按钮监听点击事件
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
        newAnnotationView.rightCalloutAccessoryView = btn;
        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        
        return newAnnotationView;
    }
    return nil;
}

 第三步:运行程序,此时大头针效果可以正常显示

 


二、POI检索

第一步:延时加载对象

- (BMKPoiSearch *)poiSearch {
    if (!_poiSearch) {
        _poiSearch = [[BMKPoiSearch alloc] init];
        _poiSearch.delegate = self;
    }
    return _poiSearch;
}

第二步:实现BMKPoiSearchDelegate代理方法

// 长按地图时会调用此方法
- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate {
    //发起检索
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = 0;
    option.pageCapacity = 20;
    option.location = coor;
    option.keyword = @"小吃";
    BOOL flag = [self.poiSearch poiSearchNearBy:option];
    if(flag) {
        NSLog(@"周边检索发送成功");
    } else {
        NSLog(@"周边检索发送失败");
    }
    
    // 设置初始化区域
    CLLocationCoordinate2D center = option.location;
    BMKCoordinateSpan span;
    span.latitudeDelta = 0.016263;
    span.longitudeDelta = 0.012334;
    BMKCoordinateRegion region;
    region.center = center;
    region.span = span;
    [self.mapView setRegion:region animated:YES];
}

 第三步:实现 BMKPoiSearchDelegate 处理回调结果

- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
//        NSLog(@"成功:%@", poiResultList.poiInfoList);
        
        [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSLog(@"%@----%@", obj.name, obj.address);  // 由于设置检索时,每页指定了10条,所以此处检索出10条相关信息
            [self addAnnoWithPT:obj.pt andTitle:obj.name andAddress:obj.address];
        }];
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    } else {
        NSLog(@"抱歉,未找到结果, %zd", error);
    }
}

 第四步:运行程序,此时便可检索到附近小吃相关标注

 注意:需要引入的头文件

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

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

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
原文地址:https://www.cnblogs.com/Jepson1218/p/5288287.html