地点检索
功能场景
支持用户根据关键词查询出行的起终点、兴趣点等,可结合路线规划、导航等功能使用。
https://mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/didianjiansuo_2.mp4
1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/地点检索@2xanidroid.png
扫码体验
使用产品
Android地图SDK|/index.php?title=androidsdk
>
POI城市检索|/index.php?title=androidsdk/guide/search/poi
Android地图SDK|/index.php?title=androidsdk
>
地点输入提示检索|/index.php?title=androidsdk/guide/search/poi
下载源码
//mapopen-website-wiki.bj.bcebos.com/demos/BaiduMapSDKExample.zip
searchInCity(PoiCitySearchOption option)
setOnGetPoiSearchResultListener(OnGetPoiSearchResultListener poiResultListener)
cityLimit(boolean cityLimit)
设置区域数据召回限制 为true时,仅召回city对应区域内数据
检索完成后需要调用PoiSearch的destroy()方法,否则会有内存泄露。
// 发起请求
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city(cityStr)
.keyword(keyWordStr)
.pageNum(mLoadIndex) // 分页编号
.cityLimit(false)
.scope(scope));
复制成功
@Override
public void onGetPoiResult(PoiResult poiResult) {
if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(PoiCitySearchActivity.this, "未找到结果", Toast.LENGTH_LONG).show();
return;
}
List<PoiInfo> poiInfos = poiResult.getAllPoi();
if (null == poiInfos) {
return;
}
// 隐藏之前的
hidePoiInfoLayout();
mRecyclerView.setVisibility(View.VISIBLE);
if (null == mPoiItemAdaper) {
mPoiItemAdaper = new PoiItemAdapter(poiInfos);
} else {
mPoiItemAdaper.updateData(poiInfos);
}
}
复制成功
https://mapopen-website-wiki.bj.bcebos.com/demos/iosVideos/地点检索@3xios.mp4
1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/ioscodes/地点检索@1xios.png
扫码体验
使用产品
iOS地图SDK|/index.php?title=iossdk
>
POI城市检索|/index.php?title=iossdk/guide/search/poi
iOS地图SDK|/index.php?title=iossdk
>
地点输入提示检索|/index.php?title=iossdk/guide/search/suggestion
下载源码
//mapopen-website-wiki.bj.bcebos.com/BaiduMapSDKDemo.zip
- (BOOL)poiSearchInCity:(BMKPOICitySearchOption*)option;
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPOISearchResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode;
BMKSuggestionSearchOption
BMKSuggestionSearchOption
BMKSuggestionSearchOption
是否只返回指定城市检索结果(默认:NO)(提示:海外区域暂不支持设置cityLimit)
- (BOOL)suggestionSearch:(BMKSuggestionSearchOption *)suggestionSearchOption;
- (void)onGetSuggestionResult:(BMKSuggestionSearch *)searcher result:(BMKSuggestionSearchResult *)result errorCode:(BMKSearchErrorCode)error;
// 根据输入发送sug检索
- (void)searchSugInfoWithKey:(NSString *)key {
if (key.length == 0) {
self.sugInfoList = [NSMutableArray array];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
});
return;
}
BMKSuggestionSearchOption *option = [BMKSuggestionSearchOption new];
option.keyword = key;
option.cityname = @"北京";
option.cityLimit = YES;
BMKSuggestionSearch *sugSearch = [BMKSuggestionSearch new];
sugSearch.delegate = self;
[sugSearch suggestionSearch:option];
}
/// 根据联想词或关键词进行POI检索
- (void)searchPOIInfoWithKey:(NSString *)key {
if (!key) {
return;
}
BMKPOICitySearchOption *option = [BMKPOICitySearchOption new];
option.keyword = key;
option.city = @"北京";
option.isCityLimit = YES;
BMKPoiSearch *search = [BMKPoiSearch new];
search.delegate = self;
[search poiSearchInCity:option];
}
#pragma mark - UISearchBarDelegate
// 输入回调
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
// sug检索
[self searchSugInfoWithKey:searchText];
}
// 确认检索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// POI检索
[self searchPOIInfoWithKey:searchBar.text];
}
复制成功
#pragma mark - BMKSuggestionSearchDelegate
/// 返回suggestion搜索结果
/// @param searcher 搜索对象
/// @param result 搜索结果
/// @param error BMKSearchErrorCode
- (void)onGetSuggestionResult:(BMKSuggestionSearch *)searcher result:(BMKSuggestionSearchResult *)result errorCode:(BMKSearchErrorCode)error {
if (error == BMK_SEARCH_NO_ERROR) {
self.sugInfoList = [result.suggestionList copy];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
});
}
}
#pragma mark - BMKPoiSearchDelegate
/// 返回POI搜索结果
/// @param searcher 搜索对象
/// @param poiResult 搜索结果列表
/// @param errorCode BMKSearchErrorCode
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPOISearchResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
[self.searchView endEditing:YES];
self.tableView.hidden = YES;
if (errorCode == BMK_SEARCH_NO_ERROR) {
if (!poiResult.poiInfoList || poiResult.poiInfoList.count == 0) {
return;
}
// 移除标注
[_mapView removeAnnotations:_mapView.annotations];
_poiInfoList = [poiResult.poiInfoList copy];
NSMutableArray<BMKPoiAnnotation *> *annotions = [NSMutableArray array];
for (int i = 0; i < poiResult.poiInfoList.count; i++) {
BMKPoiInfo *info = poiResult.poiInfoList[i];
BMKPoiAnnotation *annotaion = [[BMKPoiAnnotation alloc] init];
annotaion.coordinate = info.pt;
annotaion.title = info.name;
annotaion.index = i;
[annotions addObject:annotaion];
}
// 添加标注
[_mapView addAnnotations:annotions];
// 标注适配屏幕
[self showAnnotations:annotions animated:YES];
}
}
复制成功