ITeye博客

Android百度地图定位收索取周边在列表中展示并选择

johnnycmj · 2016-04-14 10:08 · Android · 移动开发 · 定位 · 百度地图 · 周边搜索 · Markdown

具体内容如下效果图:

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

 

[html] view plain copy

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <RelativeLayout
  6. android:id="@+id/plugin_camera_image_folder_headview"
  7. android:layout_width="fill_parent"
  8. android:layout_height="45dp"
  9. android:layout_marginBottom="3dp"
  10. android:background="#2B4058"
  11. android:gravity="center_vertical" >
  12. <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" />
  22. <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" />
  30. <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" />
  41. </RelativeLayout>
  42. <RelativeLayout
  43. android:layout_width="fill_parent"
  44. android:layout_height="fill_parent"
  45. android:layout_weight="2" >
  46. <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" />
  51. <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" />
  60. </RelativeLayout>
  61. <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" />
  66. </LinearLayout>

2、初始化控件

 

 

[java] view plain copy

 

  1. <span style="white-space:pre">        </span>dataList = new ArrayList<PoiInfo>();
  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 copy

 

  1. <span style="white-space:pre">        </span>//重新设置
  2. checkPosition = 0;
  3. adapter.setCheckposition(0);
  4. mBaiduMap = mMapView.getMap();
  5. mBaiduMap.clear();
  6. // 开启定位图层
  7. mBaiduMap.setMyLocationEnabled(true);
  8. mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(17).build()));   // 设置级别
  9. // 定位初始化
  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 copy

 

  1. public class MyLocationListener implements BDLocationListener {
  2. @Override
  3. public void onReceiveLocation(BDLocation location) {
  4. if (location == null || mMapView == null) {
  5. return;
  6. }
  7. locType = location.getLocType();
  8. Log.i("mybaidumap", "当前定位的返回值是:"+locType);
  9. 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 copy

 

  1. private void searchNeayBy(){
  2. // POI初始化搜索模块,注册搜索事件监听
  3. mPoiSearch = PoiSearch.newInstance();
  4. mPoiSearch.setOnGetPoiSearchResultListener(this);
  5. PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption();
  6. poiNearbySearchOption.keyword("公司");
  7. poiNearbySearchOption.location(new LatLng(latitude, longitude));
  8. poiNearbySearchOption.radius(100);  // 检索半径,单位是米
  9. poiNearbySearchOption.pageCapacity(20);  // 默认每页10条
  10. mPoiSearch.searchNearby(poiNearbySearchOption);  // 发起附近检索请求
  11. }

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

 

 

[java] view plain copy

 

  1. @Override
  2. public void onGetPoiResult(PoiResult result) {
  3. // 获取POI检索结果
  4. if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果
  5. Toast.makeText(MainActivity.this, "未找到结果",Toast.LENGTH_LONG).show();
  6. return;
  7. }
  8. if (result.error == SearchResult.ERRORNO.NO_ERROR) {// 检索结果正常返回
  9. //          mBaiduMap.clear();
  10. if(result != null){
  11. if(result.getAllPoi()!= null && result.getAllPoi().size()>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、源代码

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。