
上面是地图,下面是地理位置列表,有的只有地理位置列表(QQ动态的位置),这是个很常见的功能。它有个专门的叫法:POI周边搜索。
实现:
这个效果实现起来其实很简单,不过需要你先阅读下地图的API,这里使用的是高德地图的Android SDK,SDK的配置这里不作讲解,文末会放一些链接供学习。
思路:
1、利用地图的定位功能,获取用户当前的位置
2、根据获得的位置信息调用POI搜索,获取位置列表
3、ListView展示位置列表
4、用户拖动地图,获取地图中心坐标的位置信息,并执行2~3的步骤
代码:
Layout:
<LinearLayoutxmlns: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.MapViewandroid:id="@+id/map_local"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"/><ListViewandroid: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返回的结果private PoiOverlay poiOverlay;// poi图层private List<PoiItem> poiItems;// poi数据private PoiSearch_adapter adapter;@Overrideprotected 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();//设置定位监听mLocationClient.setLocationListener(this);//设置为高精度定位模式mLocationOption.setOnceLocation(true);mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//设置定位参数mLocationClient.setLocationOption(mLocationOption);mLocationClient.startLocation();}// 自定义系统定位小蓝点MyLocationStyle myLocationStyle = new MyLocationStyle();myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker));// 设置小蓝点的图标myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细aMap.setMyLocationStyle(myLocationStyle);aMap.setLocationSource(this);// 设置定位监听aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false}/** * 开始进行poi搜索 */protected void doSearchQuery() {aMap.setOnMapClickListener(null);// 进行poi搜索时清除掉地图点击事件int currentPage = 0;query = new PoiSearch.Query("", deepType, city);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)query.setPageSize(20);// 设置每页最多返回多少条poiitemquery.setPageNum(currentPage);// 设置查第一页LatLonPoint lp = new LatLonPoint(latlng.latitude, latlng.longitude);poiSearch = new PoiSearch(this, query);poiSearch.setOnPoiSearchListener(this);poiSearch.setBound(new PoiSearch.SearchBound(lp, 5000, true));// 设置搜索区域为以lp点为圆心,其周围2000米范围poiSearch.searchPOIAsyn();// 异步搜索}@Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if (mListener != null && aMapLocation != null) {if (aMapLocation.getErrorCode() == 0) {// 显示我的位置mListener.onLocationChanged(aMapLocation);//设置第一次焦点中心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);}}}@Overridepublic void activate(OnLocationChangedListener listener) {mListener = listener;mLocationClient.startLocation();}@Overridepublic void deactivate() {mListener = null;if (mLocationClient != null) {mLocationClient.stopLocation();mLocationClient.onDestroy();}mLocationClient = null;}@Overridepublic void onCameraChange(CameraPosition cameraPosition) {}@Overridepublic void onCameraChangeFinish(CameraPosition cameraPosition) {latlng = cameraPosition.target;aMap.clear();aMap.addMarker(new MarkerOptions().position(latlng));doSearchQuery();}@Overridepublic void onPoiSearched(PoiResult result, int rCode) {if (rCode == 0) {if (result != null && result.getQuery() != null) {// 搜索poi的结果if (result.getQuery().equals(query)) {// 是否是同一条poiResult = result;poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字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("无结果");}}} else {Logger.e("无结果");}} else if (rCode == 27) {Logger.e("error_network");} else if (rCode == 32) {Logger.e("error_key");} else {Logger.e("error_other:" + rCode);}}@Overridepublic void onPoiItemSearched(PoiItem poiItem, int i) {}//-------- 定位 End ------@Overrideprotected void onResume() {super.onResume();mLocationClient.startLocation();}@Overrideprotected void onPause() {super.onPause();mLocationClient.stopLocation();}@Overrideprotected void onDestroy() {mLocationClient.onDestroy();super.onDestroy();}private class mOnItemClickListener implements AdapterView.OnItemClickListener {@Overridepublic 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();}}