高德地图Api 搜索

快逃 2016-12-08 02:19:39
写了高德地图的搜索
* 开始进行poi搜索
*/
protected void doSearchQuery() {
showProgressDialog();// 显示进度框
currentPage = 0;
query = new PoiSearch.Query(keyWord, "", "北京");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(10);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);// 设置查第一页
query.setCityLimit(true);

poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
}

Marker lastCheckedMarker;
ArrayList<BitmapDescriptor> lastCheckedBitmapDescriptorList;
@Override
public void onPoiSearched(PoiResult result, int rCode) {
dissmissProgressDialog();// 隐藏对话框
if (rCode == 1000) {
if (result != null && result.getQuery() != null) {// 搜索poi的结果
if (result.getQuery().equals(query)) {// 是否是同一条
poiResult = result;

int resultPage=poiResult.getPageCount();
// 取得搜索到的poiitems有多少页
List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
List<SuggestionCity> suggestionCities = poiResult
.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
aMap.clear();
if (poiItems != null && poiItems.size() > 0) {
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
for (int j = 0; j < Math.min(poiItems.size(), 10); j++) {
Log.d("result", "===result=="+poiItems.get(j).getTitle()+"=="+poiItems.get(j).getSnippet());
//1.通过图片名称字符串,获取对应的resId.
String iconName = "poi_marker_" + (j + 1);
int iconId = getResources().getIdentifier(iconName, "drawable", this.getPackageName());
MarkerOptions markerOptions = new MarkerOptions().icon
(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
markerOptions.position(new LatLng(poiItems.get(j).getLatLonPoint().getLatitude(),
poiItems.get(j)
.getLatLonPoint().getLongitude()));
markerOptions.title(poiItems.get(j).getTitle());
markerOptions.snippet(poiItems.get(j).getSnippet());
Marker marker = aMap.addMarker(markerOptions);
//为了POI填充整个地图区域
boundsBuilder.include(new LatLng(poiItems.get(j).getLatLonPoint().getLatitude(),
poiItems.get(j)
.getLatLonPoint().getLongitude()));
// if (j == 0) {
// lastCheckedMarker = marker;
// lastCheckedBitmapDescriptorList = lastCheckedMarker.getIcons();
// ArrayList<BitmapDescriptor> bitmapDescriptorArrayList = new ArrayList<>();
// bitmapDescriptorArrayList.add(BitmapDescriptorFactory.fromResource
// (R.drawable.ic_launcher));
// marker.setIcons(bitmapDescriptorArrayList);
// }
}
LatLngBounds bounds = boundsBuilder.build();
aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
} else if (suggestionCities != null
&& suggestionCities.size() > 0) {
showSuggestCity(suggestionCities);
} else {
Toast.makeText(this, "没有结果", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(this, "没有结果", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, ""+rCode, Toast.LENGTH_SHORT).show();
}

}
最后结果可以搜索,但是如果输入 例如 国展(地铁)15号线。搜索结果有很多,不是唯一的,是包含的。 但是如果是高德的app搜索就是唯一的。谁知道哪里控制这个?
...全文
577 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧