在上一章的地圖基礎(chǔ)上,添加檢索功能
1 > 首先聲明一個(gè)檢索類和存儲(chǔ)檢索數(shù)據(jù)的數(shù)組:
@property (nonatomic, strong) BMKPoiSearch * search;
@property (nonatomic, strong) NSMutableArray * searchDataArray;
-(NSMutableArray *)searchDataArray{
if (!_searchDataArray) {
_searchDataArray = [[NSMutableArray alloc] init];
}
return _searchDataArray;
}
2 > 在didUpdateBMKUserLocation用戶位置更新的代理函數(shù)里面(設(shè)置搜索內(nèi)容狱从,參數(shù)等等):
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
[_mapView updateLocationData:userLocation];
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//更新當(dāng)前位置到地圖中間
_mapView.centerCoordinate = userLocation.location.coordinate;
//地理反編碼
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
if(flag){
NSLog(@"反geo檢索發(fā)送成功");
[_locationService stopUserLocationService];
}else{
NSLog(@"反geo檢索發(fā)送失敗");
}
//獲取用戶坐標(biāo)
_mapView.centerCoordinate = userLocation.location.coordinate;
//初始化搜索
_search = [[BMKPoiSearch alloc] init];
_search.delegate = self;
//初始化一個(gè)周邊檢索對(duì)象
BMKNearbySearchOption * option = [[BMKNearbySearchOption alloc] init];
//分頁(yè)索引,可選吊洼,默認(rèn)為0
option.pageIndex = 0;
//分頁(yè)數(shù)量,可選递沪,默認(rèn)為10综液,最多為50
option.pageCapacity = 30;
//檢索的中心點(diǎn),經(jīng)緯度
option.location = userLocation.location.coordinate;
//搜索關(guān)鍵字
option.keyword = @"餐館";
//周邊檢索半徑
option.radius = 500;
//根據(jù)中心店檩奠、半徑附帽、檢索詞等發(fā)起周邊檢索
BOOL searchFlag = [_search poiSearchNearBy:option];
if (searchFlag) {
NSLog(@"搜索成功");
//關(guān)閉檢索
[_locationService stopUserLocationService];
}else {
NSLog(@"搜索失敗");
}
}
3 > 在搜索成功之后蕉扮,會(huì)調(diào)用以下代理函數(shù):
#pragma mark -- BMKPoiSearchDelegate
/**
* 返回POI搜索結(jié)果
* @param searcher 搜索對(duì)象
* @param poiResult 搜索結(jié)果列表
* @param errorCode 錯(cuò)誤號(hào),@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);
//初始化一個(gè)點(diǎn)的注釋
BMKPointAnnotation * annotoation = [[BMKPointAnnotation alloc] init];
//坐標(biāo)
annotoation.coordinate = info.pt;
//title
annotoation.title = info.name;
//子標(biāo)題
annotoation.subtitle = info.address;
//將標(biāo)注加在地圖上
[_mapView addAnnotation:annotoation];
}
}else{
NSLog(@"檢索失敗");
}
}
/**
*返回POI詳情搜索結(jié)果
*@param searcher 搜索對(duì)象
*@param poiDetailResult 詳情搜索結(jié)果
*@param errorCode 錯(cuò)誤號(hào)屁使,@see BMKSearchErrorCode
*/
- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode {
NSLog(@"%@",poiDetailResult.name);
}
#pragma mark -- BMKMapViewDelegate
/**
*根據(jù)anntation生成對(duì)應(yīng)的View
*@param mapView 地圖View
*@param annotation 指定的標(biāo)注
*@return 生成的標(biāo)注View
*/
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
//如果是注釋點(diǎn)
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
//根據(jù)注釋點(diǎn),創(chuàng)建并初始化注釋點(diǎn)視圖
BMKPinAnnotationView *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an"];
//設(shè)置大頭針顏色
newAnnotation.pinColor = BMKPinAnnotationColorPurple;
//設(shè)置動(dòng)畫(huà)
newAnnotation.animatesDrop = YES;
return newAnnotation;
}
return nil;
}
/**
*當(dāng)選中一個(gè)annotation views(某個(gè)大頭針)時(shí),調(diào)用此接口
*@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結(jié)構(gòu)中獲取
option.poiUid = info.uid;
/**
*根據(jù)poi uid 發(fā)起poi詳情檢索
*異步函數(shù),返回結(jié)果在BMKPoiSearchDelegate的onGetPoiDetailResult通知
*@param option poi詳情檢索參數(shù)類(BMKPoiDetailSearchOption)
*@return 成功返回YES除嘹,否則返回NO
*/
BOOL flag = [self.search poiDetailSearch:option];
if (flag) {
NSLog(@"檢索成功");
} else {
NSLog(@"檢索失敗");
}
}