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

上面是地圖,下面是地理位置列表,有的只有地理位置列表(QQ動(dòng)態(tài)的位置),這是個(gè)很常見(jiàn)的功能。它有個(gè)專門(mén)的叫法:POI周邊搜索。
實(shí)現(xiàn):
這個(gè)效果實(shí)現(xiàn)起來(lái)其實(shí)很簡(jiǎn)單,不過(guò)需要你先閱讀下地圖的API,這里使用的是高德地圖的Android SDK,SDK的配置這里不作講解,文末會(huì)放一些鏈接供學(xué)習(xí)。
思路:
1、利用地圖的定位功能,獲取用戶當(dāng)前的位置
2、根據(jù)獲得的位置信息調(diào)用POI搜索,獲取位置列表
3、ListView展示位置列表
4、用戶拖動(dòng)地圖,獲取地圖中心坐標(biāo)的位置信息,并執(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)聽(tīng)
mLocationClient.setLocationListener(this);
//設(shè)置為高精度定位模式
mLocationOption.setOnceLocation(true);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設(shè)置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
}
// 自定義系統(tǒng)定位小藍(lán)點(diǎn)
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory
.fromResource(R.drawable.location_marker));// 設(shè)置小藍(lán)點(diǎn)的圖標(biāo)
myLocationStyle.strokeColor(Color.BLACK);// 設(shè)置圓形的邊框顏色
myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 設(shè)置圓形的填充顏色
myLocationStyle.strokeWidth(1.0f);// 設(shè)置圓形的邊框粗細(xì)
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 設(shè)置定位監(jiān)聽(tīng)
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設(shè)置默認(rèn)定位按鈕是否顯示
aMap.setMyLocationEnabled(true);// 設(shè)置為true表示顯示定位層并可觸發(fā)定位,false表示隱藏定位層并不可觸發(fā)定位,默認(rèn)是false
}
/**
* 開(kāi)始進(jìn)行poi搜索
*/
protected void doSearchQuery() {
aMap.setOnMapClickListener(null);// 進(jìn)行poi搜索時(shí)清除掉地圖點(diǎn)擊事件
int currentPage = 0;
query = new PoiSearch.Query("", deepType, city);// 第一個(gè)參數(shù)表示搜索字符串,第二個(gè)參數(shù)表示poi搜索類型,第三個(gè)參數(shù)表示poi搜索區(qū)域(空字符串代表全國(guó))
query.setPageSize(20);// 設(shè)置每頁(yè)最多返回多少條poiitem
query.setPageNum(currentPage);// 設(shè)置查第一頁(yè)
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ū)域?yàn)橐詌p點(diǎn)為圓心,其周?chē)?000米范圍
poiSearch.searchPOIAsyn();// 異步搜索
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mListener != null && aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
// 顯示我的位置
mListener.onLocationChanged(aMapLocation);
//設(shè)置第一次焦點(diǎn)中心
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();// 取得第一頁(yè)的poiitem數(shù)據(jù),頁(yè)數(shù)從數(shù)字0開(kāi)始
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("無(wú)結(jié)果");
}
}
} else {
Logger.e("無(wú)結(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方式啟動(dòng)的,最后點(diǎn)擊位置之后會(huì)返回點(diǎn)選的位置信息。
總結(jié):我第一次準(zhǔn)備實(shí)現(xiàn)上述的效果時(shí),也是不知所措,因?yàn)檫€沒(méi)有對(duì)地圖API有比較全面的認(rèn)識(shí),后來(lái)看了不少資料,自己便結(jié)合了一下地圖的功能點(diǎn),實(shí)現(xiàn)了設(shè)計(jì)圖中的效果。
本文作者:他叫自己MR張
本文地址:http://blog.csdn.net/ys743276112/article/details/51519223
以上就是本文的全部?jī)?nèi)容,非常感謝作者的分享,希望對(duì)大家的學(xué)習(xí)有所幫助,大家共同進(jìn)步。
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android SearchView搜索框組件的使用方法
- Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
- Android 百度地圖POI搜索功能實(shí)例代碼
- android實(shí)現(xiàn)讀取、搜索聯(lián)系人的代碼
- Android文本框搜索和清空效果實(shí)現(xiàn)代碼及簡(jiǎn)要概述
- Android撥號(hào)盤(pán) 支持T9搜索和號(hào)碼搜索等撥號(hào)盤(pán)案例
- Android搜索框通用版
- Android搜索框組件SearchView的基本使用方法
- Android實(shí)現(xiàn)搜索本地音樂(lè)的方法
相關(guān)文章
深入解讀Android的Volley庫(kù)的功能結(jié)構(gòu)
這篇文章主要介紹了Android的Volley開(kāi)發(fā)框架的功能結(jié)構(gòu),Volley是Android開(kāi)發(fā)中網(wǎng)絡(luò)部分的一大利器,包含很多HTTP協(xié)議通信的相關(guān)操作,需要的朋友可以參考下2016-05-05
Android中實(shí)現(xiàn)長(zhǎng)按照片彈出右鍵菜單功能的實(shí)例代碼
這篇文章主要介紹了Android中實(shí)現(xiàn)長(zhǎng)按照片彈出右鍵菜單功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
談?wù)剬?duì)Android View事件分發(fā)機(jī)制的理解
本篇文章主要介紹了談?wù)剬?duì)Android View事件分發(fā)機(jī)制的理解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
android實(shí)現(xiàn)左右側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)左右側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android通過(guò)多點(diǎn)觸控的方式對(duì)圖片進(jìn)行縮放的實(shí)例代碼
這篇文章主要介紹了Android通過(guò)多點(diǎn)觸控的方式對(duì)圖片進(jìn)行縮放的實(shí)例代碼,完成了點(diǎn)擊圖片就能瀏覽大圖的功能,并且在瀏覽大圖的時(shí)候還可以通過(guò)多點(diǎn)觸控的方式對(duì)圖片進(jìn)行縮放。2018-05-05
Android自定義狀態(tài)欄顏色與APP風(fēng)格保持一致的實(shí)現(xiàn)方法
我們知道iOS上的應(yīng)用,狀態(tài)欄的顏色總能與應(yīng)用標(biāo)題欄顏色保持一致,用戶體驗(yàn)很不錯(cuò),那安卓是否可以呢?下面小編給大家?guī)?lái)了Android自定義狀態(tài)欄顏色與APP風(fēng)格保持一致的實(shí)現(xiàn)方法,跟著小編一起學(xué)習(xí)吧2016-10-10
Android4.4 WebAPI實(shí)現(xiàn)拍照上傳功能
這篇文章主要介紹了Android4.4 WebAPI實(shí)現(xiàn)拍照上傳功能,本文給出4.4版本后拍照上傳的具體實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07
Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空詳解
這篇文章主要給大家介紹了關(guān)于Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

