Android實現(xiàn)帶列表的地圖POI周邊搜索功能
先看效果圖:(以公司附近的國貿(mào)為中心點)

上面是地圖,下面是地理位置列表,有的只有地理位置列表(QQ動態(tài)的位置),這是個很常見的功能。它有個專門的叫法:POI周邊搜索。
實現(xiàn):
這個效果實現(xiàn)起來其實很簡單,不過需要你先閱讀下地圖的API,這里使用的是高德地圖的Android SDK,SDK的配置這里不作講解,文末會放一些鏈接供學習。
思路:
1、利用地圖的定位功能,獲取用戶當前的位置
2、根據(jù)獲得的位置信息調(diào)用POI搜索,獲取位置列表
3、ListView展示位置列表
4、用戶拖動地圖,獲取地圖中心坐標的位置信息,并執(zhí)行2~3的步驟
代碼:
Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.amap.api.maps2d.MapView
android:id="@+id/map_local"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<ListView
android:id="@+id/map_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:divider="@color/space"
android:dividerHeight="1dp"
android:scrollbars="none"/>
</LinearLayout>
Activity:
public class New_LocalActivity extends Activity implements LocationSource,
AMapLocationListener, AMap.OnCameraChangeListener, PoiSearch.OnPoiSearchListener {
@BindView(R.id.map_local)
MapView mapView;
@BindView(R.id.map_list)
ListView mapList;
public static final String KEY_LAT = "lat";
public static final String KEY_LNG = "lng";
public static final String KEY_DES = "des";
private AMapLocationClient mLocationClient;
private LocationSource.OnLocationChangedListener mListener;
private LatLng latlng;
private String city;
private AMap aMap;
private String deepType = "";// poi搜索類型
private PoiSearch.Query query;// Poi查詢條件類
private PoiSearch poiSearch;
private PoiResult poiResult; // poi返回的結(jié)果
private PoiOverlay poiOverlay;// poi圖層
private List<PoiItem> poiItems;// poi數(shù)據(jù)
private PoiSearch_adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new__local);
ButterKnife.bind(this);
mapView.onCreate(savedInstanceState);
init();
}
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
aMap.setOnCameraChangeListener(this);
setUpMap();
}
deepType = "餐飲";//這里以餐飲為例
}
//-------- 定位 Start ------
private void setUpMap() {
if (mLocationClient == null) {
mLocationClient = new AMapLocationClient(getApplicationContext());
AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
//設(shè)置定位監(jiān)聽
mLocationClient.setLocationListener(this);
//設(shè)置為高精度定位模式
mLocationOption.setOnceLocation(true);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設(shè)置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
}
// 自定義系統(tǒng)定位小藍點
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory
.fromResource(R.drawable.location_marker));// 設(shè)置小藍點的圖標
myLocationStyle.strokeColor(Color.BLACK);// 設(shè)置圓形的邊框顏色
myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 設(shè)置圓形的填充顏色
myLocationStyle.strokeWidth(1.0f);// 設(shè)置圓形的邊框粗細
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 設(shè)置定位監(jiān)聽
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設(shè)置默認定位按鈕是否顯示
aMap.setMyLocationEnabled(true);// 設(shè)置為true表示顯示定位層并可觸發(fā)定位,false表示隱藏定位層并不可觸發(fā)定位,默認是false
}
/**
* 開始進行poi搜索
*/
protected void doSearchQuery() {
aMap.setOnMapClickListener(null);// 進行poi搜索時清除掉地圖點擊事件
int currentPage = 0;
query = new PoiSearch.Query("", deepType, city);// 第一個參數(shù)表示搜索字符串,第二個參數(shù)表示poi搜索類型,第三個參數(shù)表示poi搜索區(qū)域(空字符串代表全國)
query.setPageSize(20);// 設(shè)置每頁最多返回多少條poiitem
query.setPageNum(currentPage);// 設(shè)置查第一頁
LatLonPoint lp = new LatLonPoint(latlng.latitude, latlng.longitude);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new PoiSearch.SearchBound(lp, 5000, true));
// 設(shè)置搜索區(qū)域為以lp點為圓心,其周圍2000米范圍
poiSearch.searchPOIAsyn();// 異步搜索
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mListener != null && aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
// 顯示我的位置
mListener.onLocationChanged(aMapLocation);
//設(shè)置第一次焦點中心
latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 14), 1000, null);
city = aMapLocation.getProvince();
doSearchQuery();
} else {
String errText = "定位失敗," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
Log.e("AmapErr", errText);
}
}
}
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
mLocationClient.startLocation();
}
@Override
public void deactivate() {
mListener = null;
if (mLocationClient != null) {
mLocationClient.stopLocation();
mLocationClient.onDestroy();
}
mLocationClient = null;
}
@Override
public void onCameraChange(CameraPosition cameraPosition) {
}
@Override
public void onCameraChangeFinish(CameraPosition cameraPosition) {
latlng = cameraPosition.target;
aMap.clear();
aMap.addMarker(new MarkerOptions().position(latlng));
doSearchQuery();
}
@Override
public void onPoiSearched(PoiResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getQuery() != null) {// 搜索poi的結(jié)果
if (result.getQuery().equals(query)) {// 是否是同一條
poiResult = result;
poiItems = poiResult.getPois();// 取得第一頁的poiitem數(shù)據(jù),頁數(shù)從數(shù)字0開始
List<SuggestionCity> suggestionCities = poiResult
.getSearchSuggestionCitys();
if (poiItems != null && poiItems.size() > 0) {
adapter = new PoiSearch_adapter(this, poiItems);
mapList.setAdapter(adapter);
mapList.setOnItemClickListener(new mOnItemClickListener());
}
}
else {
Logger.d("無結(jié)果");
}
}
} else {
Logger.e("無結(jié)果");
}
} else if (rCode == 27) {
Logger.e("error_network");
} else if (rCode == 32) {
Logger.e("error_key");
} else {
Logger.e("error_other:" + rCode);
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
//-------- 定位 End ------
@Override
protected void onResume() {
super.onResume();
mLocationClient.startLocation();
}
@Override
protected void onPause() {
super.onPause();
mLocationClient.stopLocation();
}
@Override
protected void onDestroy() {
mLocationClient.onDestroy();
super.onDestroy();
}
private class mOnItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.putExtra(KEY_LAT, poiItems.get(position).getLatLonPoint().getLatitude());
intent.putExtra(KEY_LNG, poiItems.get(position).getLatLonPoint().getLongitude());
intent.putExtra(KEY_DES, poiItems.get(position).getTitle());
setResult(RESULT_OK, intent);
finish();
}
}
示例中的Activity是使用startActivityForResult方式啟動的,最后點擊位置之后會返回點選的位置信息。
總結(jié):我第一次準備實現(xiàn)上述的效果時,也是不知所措,因為還沒有對地圖API有比較全面的認識,后來看了不少資料,自己便結(jié)合了一下地圖的功能點,實現(xiàn)了設(shè)計圖中的效果。
本文作者:他叫自己MR張
本文地址:http://blog.csdn.net/ys743276112/article/details/51519223
以上就是本文的全部內(nèi)容,非常感謝作者的分享,希望對大家的學習有所幫助,大家共同進步。
相關(guān)文章
Android實現(xiàn)可播放GIF動畫的ImageView
這篇文章主要為大家詳細介紹了Android實現(xiàn)可播放GIF動畫的ImageView,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
在Flutter中制作翻轉(zhuǎn)卡片動畫的完整實例代碼
最近Flutter的勢頭是越來越猛了,作為一個Android程序猿,我自然也是想要趕緊嘗試一把,這篇文章主要給大家介紹了關(guān)于在Flutter中制作翻轉(zhuǎn)卡片動畫的相關(guān)資料,需要的朋友可以參考下2021-10-10
Android Intent調(diào)用 Uri的方法總結(jié)
這篇文章主要介紹了Android Intent調(diào)用 Uri的方法總結(jié)的相關(guān)資料,這里整理了Android Intent 調(diào)用Uri的常用方法,需要的朋友可以參考下2017-09-09

