实现百度地图显示范围内搜索关键词

效果图:
未命名.gif

实现原理: 首先,要实现范围内搜索,就得借助地图搜索类方法,去百度地图SDK里查看,发现根据范围和搜索词的方法正是我们所需要的.

屏幕快照_2016-06-16_下午3_27_11.png

接着去 BMKBoundSearchOption 里查看,发现要实现范围搜索只需要得到左下角和右上角的经纬度坐标点即可。

屏幕快照 2016-06-16 下午3.34.45.png

1.首先加载出百度地图

1
2
3
4
5
6
7

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    // 设置地图级别
    [_mapView setZoomLevel:16];
    _mapView.delegate= self;
    _mapView.isSelectedAnnotationViewFront = YES;
    [self.view addSubview:_mapView];

2.在地图加载成功后的方法里去得到左下角和右上角的坐标点的经纬度, 需要一个方法来实现屏幕坐标点转化成经纬度。

1
2
3
4
5
6
7
8
9
//加载完毕先调取一次检索
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
    leftBottomPoint = [_mapView convertPoint:CGPointMake(0,_mapView.frame.size.height) toCoordinateFromView:mapView];  // //西南角(左下角) 屏幕坐标转地理经纬度
    rightBottomPoint = [_mapView convertPoint:CGPointMake(_mapView.frame.size.width,0) toCoordinateFromView:mapView];  //东北角(右上角)同上
    //开始搜索
    [self beginSearch];
    
}

3.得到俩个点的经纬度就可以开始发起搜索了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)beginSearch{
    _poisearch = [[BMKPoiSearch alloc]init];
    _poisearch.delegate = self;
    
    BMKBoundSearchOption *boundSearchOption = [[BMKBoundSearchOption alloc]init];
    boundSearchOption.pageIndex = 0;
    boundSearchOption.pageCapacity = 20;
    boundSearchOption.keyword = @"烤鱼";
    boundSearchOption.leftBottom =leftBottomPoint;
    boundSearchOption.rightTop =rightBottomPoint;
    
    BOOL flag = [_poisearch poiSearchInbounds:boundSearchOption];
    if(flag)
    {
        NSLog(@"范围内检索发送成功");
    }
    else
    {
        NSLog(@"范围内检索发送失败");
    }
}

4.在搜索结果的代理方法里将搜索到的结果展示出来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
        [_mapView removeAnnotations:array];
        array = [NSArray arrayWithArray:_mapView.overlays];
        [_mapView removeOverlays:array];
        //在此处理正常结果
        for (int i = 0; i < result.poiInfoList.count; i++)
        {
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
            [self addAnimatedAnnotationWithName:poi.name withAddress:poi.pt];
        }
        
    } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        NSLog(@"起始点有歧义");
    } else {
        // 各种情况的判断。。。
    }
}
// 添加动画Annotation
- (void)addAnimatedAnnotationWithName:(NSString *)name withAddress:(CLLocationCoordinate2D)coor {
    
    BMKPointAnnotation*animatedAnnotation = [[BMKPointAnnotation alloc]init];
    animatedAnnotation.coordinate = coor;
    animatedAnnotation.title = name;
    [_mapView addAnnotation:animatedAnnotation];
}

5.当地图区域发生改变时,会触发的方法有3个: “正在改变”、”即将改变”、”改变完成”。

屏幕快照_2016-06-16_下午3_54_33.png

很容易就想到,我们需要使用的是”改变完成”的方法,在里面重新请求一次搜索:

1
2
3
4
5
6
7
8
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    
    leftBottomPoint = [_mapView convertPoint:CGPointMake(0,_mapView.frame.size.height) toCoordinateFromView:mapView];  // //西南角(左下角) 屏幕坐标转地理经纬度
    rightBottomPoint = [_mapView convertPoint:CGPointMake(_mapView.frame.size.width,0) toCoordinateFromView:mapView];  //东北角(右上角)同上
    
    [self beginSearch];
    
}

总结: demo只实现了一个很基础的功能,后期还可以增加更加炫酷的功能,比如改变气泡的形状。如果你有更好的想法,欢迎和我交流!
demo地址:https://github.com/wang-qiuyu/BoundsSearchDemo