POI(Point of Interest)
準(zhǔn)備
高德的搜索SDK(我用的是AMap_Search_V6.5.0_20180930.jar),這里提供高德官方文檔入口:高德搜索
實(shí)現(xiàn)
1、定義
private PoiSearch.Query query;
private PoiSearch poiSearch;
private List<String> addressNameList=new ArrayList<>();//存取標(biāo)題列表
private List<HashMap<String,Double>> addressList=new ArrayList<>();//存取經(jīng)緯度列表
2蔬蕊、實(shí)例化及使用
query = new PoiSearch.Query(searchContent, "", "");// 第一個參數(shù)表示搜索字符串癌椿,第二個參數(shù)表示poi搜索類型逾礁,第三個參數(shù)表示poi搜索區(qū)域(空字符串代表全國)
query.setPageSize(10);
query.setPageNum(0);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
3许师、回調(diào)方法里面接收數(shù)據(jù)
public void onPoiSearched(PoiResult result, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
addressList.clear();
addressNameList.clear();
if (result != null && result.getQuery() != null) {// 搜索poi的結(jié)果
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同時添加和清空數(shù)據(jù)瓤球,并保證數(shù)據(jù)源來自同一個實(shí)體溺忧;
2咏连、展示的時候直接用ListView配合ArrayAdapter,然后傳入addressNameList就行鲁森;
3祟滴、在ListView的onItemClickListener的OnItemClick()中通過position在addressList中先拿到HashMap,然后在通過鍵就能拿到經(jīng)緯度了^_^