Android必知必会-带列表的地图POI周边搜索
如果移动端访问不佳,请尝试–> Github版
2016-08-22 更新
注意:在 Activity
代码中的onPoiSearched(PoiResult result, int rCode)
方法中的 rCode
的值要根据当前使用的高德 SDK 的版本进行更改。评论中有网友说 rCode = 1000
时表示返回结果正常,我使用的版本参考了对应版本的 Demo, rCode = 0
表示正常,所以这一点使用时请务必要参考官方的 Demo。
背景
先看效果图:(以公司附近的国贸为中心点)
上面是地图,下面是地理位置列表,有的只有地理位置列表(QQ动态的位置),这是个很常见的功能。它有个专门的叫法:POI周边搜索。
实现
这个效果实现起来其实很简单,不过需要你先阅读下地图的API,这里使用的是高德地图的Android SDK,SDK的配置这里不作讲解,文末会放一些链接供学习。
思路:
- 利用地图的定位功能,获取用户当前的位置
- 根据获得的位置信息调用POI搜索,获取位置列表
- ListView展示位置列表
- 用户拖动地图,获取地图中心坐标的位置信息,并执行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有比较全面的认识,后来看了不少资料,自己便结合了一下地图的功能点,实现了设计图中的效果。
下面是一些资料,初学者务必先学习基础API的应用:
如果你有什么问题,可以在博客上留言。
PS:
Android必知必会-带列表的地图POI周边搜索的更多相关文章
- 迈向高阶:优秀Android程序员必知必会的网络基础
1.前言 网络通信一直是Android项目里比较重要的一个模块,Android开源项目上出现过很多优秀的网络框架,从一开始只是一些对HttpClient和HttpUrlConnection简易封装使用 ...
- Android必知必会-使用okhttp的PUT方式上传文件
注:如果移动端排版有问题,请看 简书版 (<-点击左边),希望CSDN能更好的支持移动端. 背景 公司的文件上传接口使用PUT协议,之前一直用的都是老项目中的上传类,现在项目中使用了okhttp ...
- Android必知必会-获取视频文件的截图、缩略图
背景 公司最近要求给我负责的APP加上视频录制和发布的功能,我简单的完成了基本的录制和视频压缩功能,后来发现发布接口需要上传视频的截图,网上搜索了一下资料,在这里整理一下. 代码实现 /** * 获取 ...
- Android程序员必知必会的网络通信传输层协议——UDP和TCP
1.点评 互联网发展至今已经高度发达,而对于互联网应用(尤其即时通讯技术这一块)的开发者来说,网络编程是基础中的基础,只有更好地理解相关基础知识,对于应用层的开发才能做到游刃有余. 对于Android ...
- SQL必知必会,带你系统学习
你一定听说过大名鼎鼎的Oracle.MySQL.MongoDB等,这些数据库都是基于一个语言标准发展起来的,那就是SQL. SQL可以帮我们在日常工作中处理各种数据,如果你是程序员.产品经理或者是运营 ...
- SQL 必知必会
本文介绍基本的 SQL 语句,包括查询.过滤.排序.分组.联结.视图.插入数据.创建操纵表等.入门系列,不足颇多,望诸君指点. 注意本文某些例子只能在特定的DBMS中实现(有的已标明,有的未标明),不 ...
- makefile 必知必会
Makefile 必知必会 Makefile的根本任务是根据规则生成目标文件. 规则 一条规则包含三个:目标文件,目标文件依赖的文件,更新(或生成)目标文件的命令. 规则: <目标文件>: ...
- mysql必知必会
春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ...
- 《MySQL必知必会》整理
目录 第1章 了解数据库 1.1 数据库基础 1.1.1 什么是数据库 1.1.2 表 1.1.3 列和数据类型 1.1.4 行 1.1.5 主键 1.2 什么是SQL 第2章 MySQL简介 2.1 ...
随机推荐
- [ZJOI 2010]Perm 排列计数
Description 题库链接 询问有多少个 \(1\sim N\) 的排列 \(P\) 满足" \(\forall i\in[2,N], P_i>P_{\frac{i}{2}}\) ...
- 幸运序列(lucky)
[问题描述] Lsy喜欢幸运数字,众所周知,幸运数字就是数字位上只有4和7的数字. 但是本题的幸运序列和幸运数字完全没关系,就是一个非常非常普通的序列.哈哈,是不是感觉被耍了,没错,你就是被耍了. L ...
- ●BZOJ 4596 [Shoi2016]黑暗前的幻想乡
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4596 题解: 容斥,矩阵树定理,矩阵行列式 先说说容斥:(一共有 N-1个公司) 令 f[i ...
- 例10-4 uva10791(唯一分解)
题意:求最小公倍数为n的数的和的最小值. 如12:(3,4),(2,6),(1,12)最小为7 要想a1,a2,a3……an的和最小,要保证他们两两互质,只要存在不互质的两个数,就一定可以近一步优化 ...
- [NOI2011]
来自FallDream的博客,未经允许,请勿转载,谢谢. 省选完挂.但是善良的教练今天丢了一套NOI2011给我们训练 6道题233(虽然一道题做过了,一道题普及组的都会,就算是4道吧) 熬了一天,总 ...
- BZOJ3817 Sum(类欧几里得算法)
设$t=\sqrt r$,原题转化为$\sum_{x=1}^n(4*\lfloor\frac{tx}2\rfloor-2*\lfloor tx\rfloor+1)$考虑如何求$\sum_{x=1}^n ...
- Python IDLE背景主题
相信刚进入python学习之路的朋友们,都还是挺喜欢python自带的IDLE,但是白的代码背景色以及其它的代码色确实让人看着有点不舒服,所以当时也琢磨着能不能自己给它换换颜色,这个当然可以,废话不多 ...
- jquery post跨域请求数据
原先一直以为要实现跨域请求只能用jsonp,只能支持GET请求,后来了解到使用POST请求也可以实现跨域,但是需要在服务器增加Access-Control-Allow-Origin和Access-Co ...
- js数组求差集
var arr1 = [2,3,5,88,99,444,66];var arr2 = [2,88,66]; arr_dive(arr1,arr2); function arr_dive(aArr,bA ...
- CSS之绝对定位
w3school定义: 绝对定位的元素的位置相对于最近的已定位祖先元素(这里的已定位指的是绝对定位或者相对定位),如果元素没有已定位的祖先元素,那么它的位置相对于最初的包含块. 对于定位的主要问题是要 ...