POI(Point of Interest)
准备
高德的搜索SDK(我用的是AMap_Search_V6.5.0_20180930.jar),这里提供高德官方文档入口:高德搜索
实现
1、定义
private PoiSearch.Query query;
private PoiSearch poiSearch;
private List<String> addressNameList=new ArrayList<>();//存取标题列表
private List<HashMap<String,Double>> addressList=new ArrayList<>();//存取经纬度列表
2、实例化及使用
query = new PoiSearch.Query(searchContent, "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(10);
query.setPageNum(0);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
3、回调方法里面接收数据
public void onPoiSearched(PoiResult result, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
addressList.clear();
addressNameList.clear();
if (result != null && result.getQuery() != null) {// 搜索poi的结果
if (result.getQuery().equals(query)) {// 是否是同一条
List<PoiItem> poiItems = poiResult.getPois();
List<SuggestionCity> suggestionCities = poiResult
.getSearchSuggestionCitys();
if (poiItems != null && poiItems.size() > 0) {
for (PoiItem item:poiItems) {
LatLonPoint point=item.getLatLonPoint();
HashMap<String,Double> map=new HashMap<>();
map.put("lng",point.getLongitude());
map.put("lat",point.getLatitude());
addressList.add(map);
addressNameList.add(item.getTitle());
}
} else if (suggestionCities != null
&& suggestionCities.size() > 0) {
for (PoiItem item:poiItems) {
LatLonPoint point=item.getLatLonPoint();
HashMap<String,Double> map=new HashMap<>();
map.put("lng",point.getLongitude());
map.put("lat",point.getLatitude());
addressList.add(map);
addressNameList.add(item.getTitle());
}
}
}
}
}
}
注意
1、addressList和addressNameList同时添加和清空数据,并保证数据源来自同一个实体;
2、展示的时候直接用ListView配合ArrayAdapter,然后传入addressNameList就行;
3、在ListView的onItemClickListener的OnItemClick()中通过position在addressList中先拿到HashMap,然后在通过键就能拿到经纬度了^_^