---
title: "Android百度地图定位收索取周边在列表中展示并选择"
author: "johnnycmj"
date: "2016-04-14T10:08:24Z"
tags: ["周边搜索", "定位", "百度地图"]
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2016-04-14T10:08:24Z"
id: 2290880
url: "https://johnnycmj.iteye.com/blog/2290880"
---

具体内容如下效果图：

![](http://img.blog.csdn.net/20160413192947151?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQv%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

1、布局文件，就是一个MapView和ListView，布局文件就是上面是一个百度地图的mapview，下面是一个显示周边位置的ListView

 

**\[html]** [view plain](http://blog.csdn.net/chmj1208/article/details/51146014 "view plain") [copy](http://blog.csdn.net/chmj1208/article/details/51146014 "copy")

 

01. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02. android:layout\_width="fill\_parent"
03. android:layout\_height="fill\_parent"
04. android:orientation="vertical" &gt;
05. &lt;RelativeLayout
06. android:id="@+id/plugin\_camera\_image\_folder\_headview"
07. android:layout\_width="fill\_parent"
08. android:layout\_height="45dp"
09. android:layout\_marginBottom="3dp"
10. android:background="#2B4058"
11. android:gravity="center\_vertical" &gt;
12. &lt;TextView
13. android:id="@+id/chat\_publish\_complete\_cancle"
14. android:layout\_width="wrap\_content"
15. android:layout\_height="wrap\_content"
16. android:layout\_alignParentLeft="true"
17. android:layout\_centerVertical="true"
18. android:layout\_marginLeft="10dp"
19. android:text="取消"
20. android:textColor="#ffffff"
21. android:textSize="16sp" /&gt;
22. &lt;TextView
23. android:id="@+id/chat\_publish\_complete\_title"
24. android:layout\_width="wrap\_content"
25. android:layout\_height="wrap\_content"
26. android:layout\_centerInParent="true"
27. android:text="选择当前位置"
28. android:textColor="#ffffff"
29. android:textSize="20sp" /&gt;
30. &lt;Button
31. android:id="@+id/chat\_publish\_complete\_publish"
32. android:layout\_width="55dp"
33. android:layout\_height="27dp"
34. android:layout\_alignParentRight="true"
35. android:layout\_centerVertical="true"
36. android:layout\_marginRight="10dp"
37. android:background="@drawable/chat\_publish\_bg"
38. android:text="完成"
39. android:textColor="#fff"
40. android:textSize="16sp" /&gt;
41. &lt;/RelativeLayout&gt;
42. &lt;RelativeLayout
43. android:layout\_width="fill\_parent"
44. android:layout\_height="fill\_parent"
45. android:layout\_weight="2" &gt;
46. &lt;com.baidu.mapapi.map.MapView
47. android:id="@+id/bmapView"
48. android:layout\_width="fill\_parent"
49. android:layout\_height="fill\_parent"
50. android:clickable="true" /&gt;
51. &lt;Button
52. android:id="@+id/request"
53. android:layout\_width="40dp"
54. android:layout\_height="40dp"
55. android:layout\_alignParentBottom="true"
56. android:layout\_alignParentLeft="true"
57. android:layout\_marginBottom="40dp"
58. android:layout\_marginLeft="10dp"
59. android:background="@drawable/custom\_loc" /&gt;
60. &lt;/RelativeLayout&gt;
61. &lt;ListView
62. android:id="@+id/lv\_location\_nearby"
63. android:layout\_width="match\_parent"
64. android:layout\_height="match\_parent"
65. android:layout\_weight="3" /&gt;
66. &lt;/LinearLayout&gt;

2、初始化控件

 

 

**\[java]** [view plain](http://blog.csdn.net/chmj1208/article/details/51146014 "view plain") [copy](http://blog.csdn.net/chmj1208/article/details/51146014 "copy")

 

1. &lt;span style="white-space:pre"&gt;        &lt;/span&gt;dataList = new ArrayList&lt;PoiInfo&gt;();
2. mMapView = (MapView) findViewById(R.id.bmapView);
3. mCompleteButton = (Button) findViewById(R.id.chat\_publish\_complete\_publish);
4. mRequestLocation = (Button) findViewById(R.id.request);
5. mListView = (ListView) findViewById(R.id.lv\_location\_nearby);
6. checkPosition=0;
7. adapter = new ListAdapter(0);
8. mListView.setAdapter(adapter);

3、 定位

 

 

**\[java]** [view plain](http://blog.csdn.net/chmj1208/article/details/51146014 "view plain") [copy](http://blog.csdn.net/chmj1208/article/details/51146014 "copy")

 

01. &lt;span style="white-space:pre"&gt;        &lt;/span&gt;//重新设置
02. checkPosition = 0;
03. adapter.setCheckposition(0);
04. mBaiduMap = mMapView.getMap();
05. mBaiduMap.clear();
06. // 开启定位图层
07. mBaiduMap.setMyLocationEnabled(true);
08. mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(17).build()));   // 设置级别
09. // 定位初始化
10. mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
11. mLocationClient.registerLocationListener(myListener);// 注册定位监听接口
12. /\**
13. * 设置定位参数
14. \*/
15. LocationClientOption option = new LocationClientOption();
16. option.setLocationMode(LocationMode.Hight\_Accuracy);// 设置定位模式
17. //      option.setScanSpan(5000);// 设置发起定位请求的间隔时间,ms
18. option.setNeedDeviceDirect(true);// 设置返回结果包含手机的方向
19. option.setOpenGps(true);
20. option.setAddrType("all");// 返回的定位结果包含地址信息
21. option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
22. option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
23. mLocationClient.setLocOption(option);
24. mLocationClient.start(); // 调用此方法开始定位

4、定位SDK监听函数

 

 

**\[java]** [view plain](http://blog.csdn.net/chmj1208/article/details/51146014 "view plain") [copy](http://blog.csdn.net/chmj1208/article/details/51146014 "copy")

 

01. public class MyLocationListener implements BDLocationListener {
02. @Override
03. public void onReceiveLocation(BDLocation location) {
04. if (location == null || mMapView == null) {
05. return;
06. }
07. locType = location.getLocType();
08. Log.i("mybaidumap", "当前定位的返回值是："+locType);
09. longitude = location.getLongitude();
10. latitude = location.getLatitude();
11. if (location.hasRadius()) {// 判断是否有定位精度半径
12. radius = location.getRadius();
13. }
14. if (locType == BDLocation.TypeNetWorkLocation) {
15. addrStr = location.getAddrStr();// 获取反地理编码(文字描述的地址)
16. Log.i("mybaidumap", "当前定位的地址是："+addrStr);
17. }
18. direction = location.getDirection();// 获取手机方向，【0~360°】,手机上面正面朝北为0°
19. province = location.getProvince();// 省份
20. city = location.getCity();// 城市
21. district = location.getDistrict();// 区县
22. LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
23. //将当前位置加入List里面
24. PoiInfo info = new PoiInfo();
25. info.address = location.getAddrStr();
26. info.city = location.getCity();
27. info.location = ll;
28. info.name = location.getAddrStr();
29. dataList.add(info);
30. adapter.notifyDataSetChanged();
31. Log.i("mybaidumap", "province是："+province +" city是"+city +" 区县是: "+district);
32. // 构造定位数据
33. MyLocationData locData = new MyLocationData.Builder()
34. .accuracy(location.getRadius())
35. // 此处设置开发者获取到的方向信息，顺时针0-360
36. .direction(100).latitude(location.getLatitude())
37. .longitude(location.getLongitude()).build();
38. mBaiduMap.setMyLocationData(locData);
39. //画标志
40. CoordinateConverter converter = new CoordinateConverter();
41. converter.coord(ll);
42. converter.from(CoordinateConverter.CoordType.COMMON);
43. LatLng convertLatLng = converter.convert();
44. OverlayOptions ooA = new MarkerOptions().position(ll).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon\_marka));
45. mCurrentMarker = (Marker) mBaiduMap.addOverlay(ooA);
46. MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 17.0f);
47. mBaiduMap.animateMapStatus(u);
48. //画当前定位标志
49. MapStatusUpdate uc = MapStatusUpdateFactory.newLatLng(ll);
50. mBaiduMap.animateMapStatus(uc);
51. mMapView.showZoomControls(false);
52. //poi 搜索周边
53. new Thread(new Runnable() {
54. @Override
55. public void run() {
56. // TODO Auto-generated method stub
57. Looper.prepare();
58. searchNeayBy();
59. Looper.loop();
60. }
61. }).start();
62. }

5、搜索周边：

 

 

**\[java]** [view plain](http://blog.csdn.net/chmj1208/article/details/51146014 "view plain") [copy](http://blog.csdn.net/chmj1208/article/details/51146014 "copy")

 

01. private void searchNeayBy(){
02. // POI初始化搜索模块，注册搜索事件监听
03. mPoiSearch = PoiSearch.newInstance();
04. mPoiSearch.setOnGetPoiSearchResultListener(this);
05. PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption();
06. poiNearbySearchOption.keyword("公司");
07. poiNearbySearchOption.location(new LatLng(latitude, longitude));
08. poiNearbySearchOption.radius(100);  // 检索半径，单位是米
09. poiNearbySearchOption.pageCapacity(20);  // 默认每页10条
10. mPoiSearch.searchNearby(poiNearbySearchOption);  // 发起附近检索请求
11. }

7、周边地理位置结果返回

 

 

**\[java]** [view plain](http://blog.csdn.net/chmj1208/article/details/51146014 "view plain") [copy](http://blog.csdn.net/chmj1208/article/details/51146014 "copy")

 

01. @Override
02. public void onGetPoiResult(PoiResult result) {
03. // 获取POI检索结果
04. if (result == null || result.error == SearchResult.ERRORNO.RESULT\_NOT\_FOUND) {// 没有找到检索结果
05. Toast.makeText(MainActivity.this, "未找到结果",Toast.LENGTH\_LONG).show();
06. return;
07. }
08. if (result.error == SearchResult.ERRORNO.NO\_ERROR) {// 检索结果正常返回
09. //          mBaiduMap.clear();
10. if(result != null){
11. if(result.getAllPoi()!= null && result.getAllPoi().size()&gt;0){
12. dataList.addAll(result.getAllPoi());
13. //                  adapter.notifyDataSetChanged();
14. Message msg = new Message();
15. msg.what = 0;
16. handler.sendMessage(msg);
17. }
18. }
19. }
20. }

8、返回结果result.getAllPoi() 设到ListView的Adapter里面去，刷新控件即可。

[**9、源代码**](http://download.csdn.net/detail/chmj1208/9490238)