效果图:
代码:
1.导入头文件
#import <BaiduMapAPI_Search/BMKPoiSearch.h>
<BMKPoiSearchDelegate.h>
BMKPoiSearch *_searcher;
_searcher =[[BMKPoiSearchalloc]init];
_searcher.delegate =self;
//发起检索
BMKNearbySearchOption *option = [[BMKNearbySearchOptionalloc]init];
option.pageCapacity = 10;
option.location = coor;
option.keyword = @"小吃";
BOOL flag = [_searcherpoiSearchNearBy:option];
if(flag)
{
NSLog(@"周边检索发送成功");
}
else
{
NSLog(@"周边检索发送失败");
}
//实现PoiSearchDeleage处理回调结果
- (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];
}];
}
elseif (error ==BMK_SEARCH_AMBIGUOUS_KEYWORD){
//当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
// result.cityList;
NSLog(@"起始点有歧义");
} else {
NSLog(@"抱歉,未找到结果");
}
}
//不使用时将delegate设置为 nil
-(void)viewWillDisappear:(BOOL)animated
{
_searcher.delegate =nil;
}
//添加大头针
- (void)addAnnoWithPT:(CLLocationCoordinate2D)pt andTitle:(NSString *)name andAddress:(NSString *)address{
BMKPointAnnotation *pointAnnotations = [[BMKPointAnnotationalloc]init];
CLLocationCoordinate2D coords;
coords.latitude = pt.latitude;
coords.longitude = pt.longitude;
pointAnnotations.coordinate = coords;
pointAnnotations.title = @"巴士互联欢迎您!";
[_mapView addAnnotation:pointAnnotations];
}