地点搜索(Search接口),提供三类范围条件的搜索功能:
1、指定城市的地点搜索:如在北京搜索餐馆;
2、圆形区域搜索:一般用于指定位置的周边(附近)地点搜索:如,搜索颐和园附近的酒店;
3、矩形区域的地点搜索:在地图应用中,往往用于视野内搜索,因为显示地图的区域是个矩形。
修改您的ViewController.m文件,添加以下代码,使您的ViewController实现QMSSearchDelegate协议:并添加搜索服务的对象QMSSearcher* searcher,可以通过使用如下方式设置POI检索参数:
@interface ViewController () @property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) QMSSearcher *searcher;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setUI];
}
- (void)setUI {
double width = self.view.bounds.size.width;
double height = 50;
[self.navigationController.navigationBar setHidden:true];
double y = self.navigationController.navigationBar.frame.origin.y;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, y, width, height)];
[self.searchBar setDelegate:self];
[self.view addSubview:self.searchBar];
self.searcher = [[QMSSearcher alloc] init];
[self.searcher setDelegate:self];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
//地区检索
[poiSearchOption setBoundaryByRegionWithCityName:@"北京" autoExtend:NO];
//周边检索
//[poiSearchOption setBoundaryByNearbyWithCenterCoordinate:CLLocationCoordinate2DMake(39, 116) radius:1000];
//矩形检索
//[poiSearchOption setBoundaryByRectangleWithleftBottomCoordinate:
//CLLocationCoordinate2DMake(39, 116) rightTopCoordinate:
//CLLocationCoordinate2DMake(40, 117)];
//设置检索分类
[poiSearchOption setFilter:@"category=美食"];
[poiSearchOption setKeyword:searchText];
[self.searcher searchWithPoiSearchOption:poiSearchOption];
}
当发起检索后,可以通过如下回调函数获取检索结果
//查询出现错误
- (void)searchWithSearchOption:(QMSSearchOption *)searchOption
didFailWithError:(NSError*)error;
//poi查询结果回调函数
- (void)searchWithPoiSearchOption:(QMSPoiSearchOption *)poiSearchOption
didReceiveResult:(QMSPoiSearchResult *)poiSearchResult;
更详细的代码示例请参考QmapSearchDemo中的PoiSearchViewController类
效果图如下: