iOS - 百度地图二(添加大头针)

在上一章的地图基础上,添加检索功能

1 > 首先声明一个检索类和存储检索数据的数组:
@property (nonatomic, strong) BMKPoiSearch       * search;

@property (nonatomic, strong) NSMutableArray     * searchDataArray;

-(NSMutableArray *)searchDataArray{

if (!_searchDataArray) {
    
    _searchDataArray = [[NSMutableArray alloc] init];
}

  return  _searchDataArray;
}
2 > 在didUpdateBMKUserLocation用户位置更新的代理函数里面(设置搜索内容,参数等等):
 - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{

  [_mapView updateLocationData:userLocation];

  NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

  //更新当前位置到地图中间
  _mapView.centerCoordinate = userLocation.location.coordinate;

  //地理反编码
  BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

  reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;

  BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];

  if(flag){
    
      NSLog(@"反geo检索发送成功");
    
      [_locationService stopUserLocationService];
    
  }else{
    
      NSLog(@"反geo检索发送失败");
    
  }


  //获取用户坐标
  _mapView.centerCoordinate = userLocation.location.coordinate;
  //初始化搜索
  _search = [[BMKPoiSearch alloc] init];
  _search.delegate = self;
  //初始化一个周边检索对象
  BMKNearbySearchOption * option = [[BMKNearbySearchOption alloc] init];
  //分页索引,可选,默认为0
  option.pageIndex = 0;
  //分页数量,可选,默认为10,最多为50
  option.pageCapacity = 30;
  //检索的中心点,经纬度
  option.location = userLocation.location.coordinate;
  //搜索关键字
  option.keyword = @"餐馆";
  //周边检索半径
  option.radius = 500;

  //根据中心店、半径、检索词等发起周边检索
  BOOL searchFlag = [_search poiSearchNearBy:option];
     if (searchFlag) {
    
          NSLog(@"搜索成功");
          //关闭检索
        [_locationService stopUserLocationService];

      }else {
    
          NSLog(@"搜索失败");
    }

}
3 > 在搜索成功之后,会调用以下代理函数:
#pragma mark -- BMKPoiSearchDelegate
/**
 * 返回POI搜索结果
 * @param searcher 搜索对象
 * @param poiResult 搜索结果列表
 * @param errorCode 错误号,@see BMKSearchErrorCode
 */
-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode{

    //检索成功
    if (errorCode == BMK_SEARCH_NO_ERROR) {
    
        //POI信息类(Point of Interest)
        for (BMKPoiInfo * info in poiResult.poiInfoList) {
        
            [self.searchDataArray addObject:info];
        
          //             NSLog(@"info.name = %@",info.name);
            //初始化一个点的注释
          BMKPointAnnotation * annotoation = [[BMKPointAnnotation alloc] init];
        
          //坐标
          annotoation.coordinate = info.pt;
        
          //title
          annotoation.title = info.name;
        
          //子标题
          annotoation.subtitle = info.address;
        
          //将标注加在地图上
          [_mapView addAnnotation:annotoation];
        
      }
    }else{
    
        NSLog(@"检索失败");
    }
}

/**
 *返回POI详情搜索结果
 *@param searcher 搜索对象
 *@param poiDetailResult 详情搜索结果
 *@param errorCode 错误号,@see BMKSearchErrorCode
 */
- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode {

    NSLog(@"%@",poiDetailResult.name);

}


#pragma mark -- BMKMapViewDelegate
/**
 *根据anntation生成对应的View
 *@param mapView 地图View
 *@param annotation 指定的标注
 *@return 生成的标注View
 */
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{

      //如果是注释点
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
    
        //根据注释点,创建并初始化注释点视图
        BMKPinAnnotationView  *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an"];
    
        //设置大头针颜色
        newAnnotation.pinColor = BMKPinAnnotationColorPurple;
    
        //设置动画
        newAnnotation.animatesDrop = YES;
      
        return newAnnotation;
    }

    return nil;

}

 /**
 *当选中一个annotation views(某个大头针)时,调用此接口
 *@param mapView 地图View
 *@param views 选中的annotation views
 */
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view {

    //poi详情检索信息类
    BMKPoiDetailSearchOption *option = [[BMKPoiDetailSearchOption alloc] init];

    BMKPoiInfo *info = self.searchDataArray.firstObject;

    //poi的uid,从poi检索返回的BMKPoiResult结构中获取
    option.poiUid = info.uid;

 /**
 *根据poi uid 发起poi详情检索
 *异步函数,返回结果在BMKPoiSearchDelegate的onGetPoiDetailResult通知
 *@param option poi详情检索参数类(BMKPoiDetailSearchOption)
 *@return 成功返回YES,否则返回NO
 */
BOOL flag = [self.search poiDetailSearch:option];

    if (flag) {
        NSLog(@"检索成功");

   } else {
    
        NSLog(@"检索失败");
    }
}
4 > 以上设置,就可以了,运行得带如下图结果:
iOS - 百度地图二(添加大头针)_第1张图片
IMG_7882.PNG

你可能感兴趣的:(iOS - 百度地图二(添加大头针))