實(shí)現(xiàn)百度地圖顯示范圍內(nèi)搜索關(guān)鍵詞
效果圖:
未命名.gif
實(shí)現(xiàn)原理: 首先,要實(shí)現(xiàn)范圍內(nèi)搜索,就得借助地圖搜索類方法,去百度地圖SDK里查看,發(fā)現(xiàn)根據(jù)范圍和搜索詞的方法正是我們所需要的.
屏幕快照_2016-06-16_下午3_27_11.png
接著去 BMKBoundSearchOption 里查看,發(fā)現(xiàn)要實(shí)現(xiàn)范圍搜索只需要得到左下角和右上角的經(jīng)緯度坐標(biāo)點(diǎn)即可。
屏幕快照 2016-06-16 下午3.34.45.png
1.首先加載出百度地圖
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
// 設(shè)置地圖級(jí)別
[_mapView setZoomLevel:16];
_mapView.delegate= self;
_mapView.isSelectedAnnotationViewFront = YES;
[self.view addSubview:_mapView];
2.在地圖加載成功后的方法里去得到左下角和右上角的坐標(biāo)點(diǎn)的經(jīng)緯度, 需要一個(gè)方法來(lái)實(shí)現(xiàn)屏幕坐標(biāo)點(diǎn)轉(zhuǎn)化成經(jīng)緯度。
//加載完畢先調(diào)取一次檢索
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
leftBottomPoint = [_mapView convertPoint:CGPointMake(0,_mapView.frame.size.height) toCoordinateFromView:mapView]; // //西南角(左下角) 屏幕坐標(biāo)轉(zhuǎn)地理經(jīng)緯度
rightBottomPoint = [_mapView convertPoint:CGPointMake(_mapView.frame.size.width,0) toCoordinateFromView:mapView]; //東北角(右上角)同上
//開始搜索
[self beginSearch];
}
3.得到倆個(gè)點(diǎn)的經(jīng)緯度就可以開始發(fā)起搜索了。
- (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(@"范圍內(nèi)檢索發(fā)送成功");
}
else
{
NSLog(@"范圍內(nèi)檢索發(fā)送失敗");
}
}
4.在搜索結(jié)果的代理方法里將搜索到的結(jié)果展示出來(lái)。
#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];
//在此處理正常結(jié)果
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(@"起始點(diǎn)有歧義");
} else {
// 各種情況的判斷。。。
}
}
// 添加動(dòng)畫Annotation
- (void)addAnimatedAnnotationWithName:(NSString *)name withAddress:(CLLocationCoordinate2D)coor {
BMKPointAnnotation*animatedAnnotation = [[BMKPointAnnotation alloc]init];
animatedAnnotation.coordinate = coor;
animatedAnnotation.title = name;
[_mapView addAnnotation:animatedAnnotation];
}
5.當(dāng)?shù)貓D區(qū)域發(fā)生改變時(shí),會(huì)觸發(fā)的方法有3個(gè): "正在改變"、"即將改變"、"改變完成"。
屏幕快照_2016-06-16_下午3_54_33.png
很容易就想到,我們需要使用的是"改變完成"的方法,在里面重新請(qǐng)求一次搜索:
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
leftBottomPoint = [_mapView convertPoint:CGPointMake(0,_mapView.frame.size.height) toCoordinateFromView:mapView]; // //西南角(左下角) 屏幕坐標(biāo)轉(zhuǎn)地理經(jīng)緯度
rightBottomPoint = [_mapView convertPoint:CGPointMake(_mapView.frame.size.width,0) toCoordinateFromView:mapView]; //東北角(右上角)同上
[self beginSearch];
}
**
總結(jié): demo只實(shí)現(xiàn)了一個(gè)很基礎(chǔ)的功能,后期還可以增加更加炫酷的功能,比如改變氣泡的形狀。如果你有更好的想法,歡迎和我交流!
**
demo地址:
https://github.com/xiaochenyi/BoundsSearchDemo