Android实现带列表的地图POI周边搜索功能
作者:他叫自己MR张 发布时间:2022-09-17 02:48:35
标签:Android,地图,POI,搜索
先看效果图:(以公司附近的国贸为中心点)
上面是地图,下面是地理位置列表,有的只有地理位置列表(QQ动态的位置),这是个很常见的功能。它有个专门的叫法:POI周边搜索。
实现:
这个效果实现起来其实很简单,不过需要你先阅读下地图的API,这里使用的是高德地图的Android SDK,SDK的配置这里不作讲解,文末会放一些链接供学习。
思路:
1、利用地图的定位功能,获取用户当前的位置
2、根据获得的位置信息调用POI搜索,获取位置列表
3、ListView展示位置列表
4、用户拖动地图,获取地图中心坐标的位置信息,并执行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返回的结果
private PoiOverlay poiOverlay;// poi图层
private List<PoiItem> poiItems;// poi数据
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();
//设置定位监听
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);// 设置每页最多返回多少条poiitem
query.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();// 异步搜索
}
@Override
public 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);
}
}
}
@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的结果
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);
}
}
@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方式启动的,最后点击位置之后会返回点选的位置信息。
总结:我第一次准备实现上述的效果时,也是不知所措,因为还没有对地图API有比较全面的认识,后来看了不少资料,自己便结合了一下地图的功能点,实现了设计图中的效果。
本文作者:他叫自己MR张
本文地址:http://blog.csdn.net/ys743276112/article/details/51519223


猜你喜欢
- File 类:文件和目录路径名的抽象表示。注意:File 类只能操作文件的属性,文件的内容是不能操作的。1、File 类的字段我们知道,各个
- 这篇文章主要介绍了Spring框架实现AOP添加日志记录功能过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 本文实例为大家分享了Android实现聊天界面的具体代码,供大家参考,具体内容如下文件目录在app下的build.gradle中添加依赖库(
- 效果图片重写DataGridView的OnRowPostPaint方法或者直接在DataGridView的RowPostPaint事件里写,
- 一、@EnableTransactionManagement工作原理开启Spring事务本质上就是增加了一个Advisor,但我们使用 @E
- 1. strlen —— 求字符串长度1.1 strlen 的声明与用处strlen ,我们有一些英
- 1.类加载机制Java中的源码.java后缀文件会在运行前被编译成.class后缀文件,文件内的字节码的本质就是一个字节数组 ,它有特定的复
- 目录前言系统调用的分类同步回调实例异步回调实例基于Future的半异步小结前言先让我们通过一个生活中的场景来还原一下回调的场景:你遇到了一个
- 一、题目描述给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种
- spring xml中配置视图如果是如下<property name="defaultViews"><
- shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和
- 一、效果实现二、实现思路:1. 正上方的提示区域,用一个类(LockIndicator.java)来实现,自定义view来绘制9个提示图标;
- 本文主要介绍了25行Java代码将普通图片转换为字符画图片和文本的实现,分享给大家,具体如下:原图生成字符画文本(像素转换字符显示后,打开字
- 1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取,因为包含文件的上传,所以采
- 问题描述Spring Cache提供的@Cacheable注解不支持配置过期时间,还有缓存的自动刷新。我们可以通过配置CacheManneg
- RocketMQ发送消息我们在使用RocketMQ发送消息时,一般都会使用DefaultMQProducer,类型的代码如下:Default
- 以下通过3个知识点给大家讲解了上拉加载和下拉刷新的Fragment实现的方法,在对每个知识点介绍了一下用法。1.效果预览1.1.这个首页就是
- Servlet3.0的出现是servlet史上最大的变革,其中的许多新特性大大的简化了web应用的开发,为广大劳苦的程序员减轻了压力,提高了
- UI 妹纸又给了个图叫我做,我一看是这样的:我们首先把这个控件划分成 几个部分:1.底下部分的直线 :2.左右两边的半圆
- LRU简介LRU是Least Recently Used 近期最少使用算法,它就可以将长时间没有被利用的数据进行删除。实现最近面了阿里的外包