当前位置: 代码迷 >> Android >> Android百度map定位收索取周边在列表中展示并选择
  详细解决方案

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

热度:215   发布时间:2016-04-24 11:08:26.0
Android百度地图定位收索取周边在列表中展示并选择

具体内容如下效果图:


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. ??
  6. ????<RelativeLayout??
  7. ????????android:id="@+id/plugin_camera_image_folder_headview"??
  8. ????????android:layout_width="fill_parent"??
  9. ????????android:layout_height="45dp"??
  10. ????????android:layout_marginBottom="3dp"??
  11. ????????android:background="#2B4058"??
  12. ????????android:gravity="center_vertical"?>??
  13. ??
  14. ????????<TextView??
  15. ????????????android:id="@+id/chat_publish_complete_cancle"??
  16. ????????????android:layout_width="wrap_content"??
  17. ????????????android:layout_height="wrap_content"??
  18. ????????????android:layout_alignParentLeft="true"??
  19. ????????????android:layout_centerVertical="true"??
  20. ????????????android:layout_marginLeft="10dp"??
  21. ????????????android:text="取消"??
  22. ????????????android:textColor="#ffffff"??
  23. ????????????android:textSize="16sp"?/>??
  24. ??
  25. ????????<TextView??
  26. ????????????android:id="@+id/chat_publish_complete_title"??
  27. ????????????android:layout_width="wrap_content"??
  28. ????????????android:layout_height="wrap_content"??
  29. ????????????android:layout_centerInParent="true"??
  30. ????????????android:text="选择当前位置"??
  31. ????????????android:textColor="#ffffff"??
  32. ????????????android:textSize="20sp"?/>??
  33. ??
  34. ????????<Button??
  35. ????????????android:id="@+id/chat_publish_complete_publish"??
  36. ????????????android:layout_width="55dp"??
  37. ????????????android:layout_height="27dp"??
  38. ????????????android:layout_alignParentRight="true"??
  39. ????????????android:layout_centerVertical="true"??
  40. ????????????android:layout_marginRight="10dp"??
  41. ????????????android:background="@drawable/chat_publish_bg"??
  42. ????????????android:text="完成"??
  43. ????????????android:textColor="#fff"??
  44. ????????????android:textSize="16sp"?/>??
  45. ????</RelativeLayout>??
  46. ??
  47. ????<RelativeLayout??
  48. ????????android:layout_width="fill_parent"??
  49. ????????android:layout_height="fill_parent"??
  50. ????????android:layout_weight="2"?>??
  51. ??
  52. ????????<com.baidu.mapapi.map.MapView??
  53. ????????????android:id="@+id/bmapView"??
  54. ????????????android:layout_width="fill_parent"??
  55. ????????????android:layout_height="fill_parent"??
  56. ????????????android:clickable="true"?/>??
  57. ??
  58. ????????<Button??
  59. ????????????android:id="@+id/request"??
  60. ????????????android:layout_width="40dp"??
  61. ????????????android:layout_height="40dp"??
  62. ????????????android:layout_alignParentBottom="true"??
  63. ????????????android:layout_alignParentLeft="true"??
  64. ????????????android:layout_marginBottom="40dp"??
  65. ????????????android:layout_marginLeft="10dp"??
  66. ????????????android:background="@drawable/custom_loc"?/>??
  67. ????</RelativeLayout>??
  68. ??
  69. ????<ListView??
  70. ????????android:id="@+id/lv_location_nearby"??
  71. ????????android:layout_width="match_parent"??
  72. ????????android:layout_height="match_parent"??
  73. ????????android:layout_weight="3"?/>??
  74. ??
  75. </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. ??????????
  5. ????????mBaiduMap?=?mMapView.getMap();??
  6. ????????mBaiduMap.clear();??
  7. ????????//?开启定位图层??
  8. ????????mBaiduMap.setMyLocationEnabled(true);??
  9. ????????mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new?MapStatus.Builder().zoom(17).build()));???//?设置级别??
  10. ??????????
  11. ????????//?定位初始化??
  12. ????????mLocationClient?=?new?LocationClient(getApplicationContext());?//?声明LocationClient类??
  13. ????????mLocationClient.registerLocationListener(myListener);//?注册定位监听接口??
  14. ??????????
  15. ????????/**?
  16. ?????????*?设置定位参数?
  17. ?????????*/??
  18. ????????LocationClientOption?option?=?new?LocationClientOption();??
  19. ????????option.setLocationMode(LocationMode.Hight_Accuracy);//?设置定位模式??
  20. //??????option.setScanSpan(5000);//?设置发起定位请求的间隔时间,ms??
  21. ????????option.setNeedDeviceDirect(true);//?设置返回结果包含手机的方向??
  22. ????????option.setOpenGps(true);??
  23. ????????option.setAddrType("all");//?返回的定位结果包含地址信息??
  24. ????????option.setCoorType("bd09ll");//?返回的定位结果是百度经纬度,默认值gcj02??
  25. ????????option.setIsNeedAddress(true);//?返回的定位结果包含地址信息??
  26. ????????mLocationClient.setLocOption(option);??
  27. ????????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. ??????????????
  8. ????????????locType?=?location.getLocType();??
  9. ????????????Log.i("mybaidumap",?"当前定位的返回值是:"+locType);??
  10. ??????????????
  11. ????????????longitude?=?location.getLongitude();??
  12. ????????????latitude?=?location.getLatitude();??
  13. ????????????if?(location.hasRadius())?{//?判断是否有定位精度半径??
  14. ????????????????radius?=?location.getRadius();??
  15. ????????????}??
  16. ??????????????
  17. ????????????if?(locType?==?BDLocation.TypeNetWorkLocation)?{??
  18. ????????????????addrStr?=?location.getAddrStr();//?获取反地理编码(文字描述的地址)??
  19. ????????????????Log.i("mybaidumap",?"当前定位的地址是:"+addrStr);??
  20. ????????????}??
  21. ??????????????
  22. ????????????direction?=?location.getDirection();//?获取手机方向,【0~360°】,手机上面正面朝北为0°??
  23. ????????????province?=?location.getProvince();//?省份??
  24. ????????????city?=?location.getCity();//?城市??
  25. ????????????district?=?location.getDistrict();//?区县??
  26. ??????????????
  27. ????????????LatLng?ll?=?new?LatLng(location.getLatitude(),location.getLongitude());??
  28. ??????????????
  29. ????????????//将当前位置加入List里面??
  30. ????????????PoiInfo?info?=?new?PoiInfo();??
  31. ????????????info.address?=?location.getAddrStr();??
  32. ????????????info.city?=?location.getCity();??
  33. ????????????info.location?=?ll;??
  34. ????????????info.name?=?location.getAddrStr();??
  35. ????????????dataList.add(info);??
  36. ????????????adapter.notifyDataSetChanged();??
  37. ????????????Log.i("mybaidumap",?"province是:"+province?+"?city是"+city?+"?区县是:?"+district);??
  38. ??
  39. ??????????????
  40. ????????????//?构造定位数据??
  41. ????????????MyLocationData?locData?=?new?MyLocationData.Builder()??
  42. ????????????????????.accuracy(location.getRadius())??
  43. ????????????????????//?此处设置开发者获取到的方向信息,顺时针0-360??
  44. ????????????????????.direction(100).latitude(location.getLatitude())??
  45. ????????????????????.longitude(location.getLongitude()).build();??
  46. ????????????mBaiduMap.setMyLocationData(locData);??
  47. ??????????????
  48. ????????????//画标志??
  49. ????????????CoordinateConverter?converter?=?new?CoordinateConverter();??
  50. ????????????converter.coord(ll);??
  51. ????????????converter.from(CoordinateConverter.CoordType.COMMON);??
  52. ????????????LatLng?convertLatLng?=?converter.convert();??
  53. ??????????????
  54. ????????????OverlayOptions?ooA?=?new?MarkerOptions().position(ll).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka));??
  55. ????????????mCurrentMarker?=?(Marker)?mBaiduMap.addOverlay(ooA);??
  56. ???????????????
  57. ??????????????
  58. ?????????????MapStatusUpdate?u?=?MapStatusUpdateFactory.newLatLngZoom(convertLatLng,?17.0f);??
  59. ?????????????mBaiduMap.animateMapStatus(u);??
  60. ???????????????
  61. ?????????????//画当前定位标志??
  62. ?????????????MapStatusUpdate?uc?=?MapStatusUpdateFactory.newLatLng(ll);??
  63. ?????????????mBaiduMap.animateMapStatus(uc);??
  64. ??????????????
  65. ?????????????mMapView.showZoomControls(false);??
  66. ?????????????//poi?搜索周边??
  67. ?????????????new?Thread(new?Runnable()?{??
  68. ????????????????@Override??
  69. ????????????????public?void?run()?{??
  70. ????????????????????//?TODO?Auto-generated?method?stub??
  71. ????????????????????Looper.prepare();???
  72. ????????????????????searchNeayBy();??
  73. ????????????????????Looper.loop();??
  74. ????????????????}??
  75. ????????????}).start();??
  76. ???????????????
  77. ??
  78. ????????}??


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. ??
  7. ????????poiNearbySearchOption.keyword("公司");??
  8. ????????poiNearbySearchOption.location(new?LatLng(latitude,?longitude));??
  9. ????????poiNearbySearchOption.radius(100);??//?检索半径,单位是米??
  10. ????????poiNearbySearchOption.pageCapacity(20);??//?默认每页10条??
  11. ????????mPoiSearch.searchNearby(poiNearbySearchOption);??//?发起附近检索请求??
  12. ????}??

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. ??
  9. ????????if?(result.error?==?SearchResult.ERRORNO.NO_ERROR)?{//?检索结果正常返回??
  10. //??????????mBaiduMap.clear();??
  11. ????????????if(result?!=?null){??
  12. ????????????????if(result.getAllPoi()!=?null?&&?result.getAllPoi().size()>0){??
  13. ????????????????????dataList.addAll(result.getAllPoi());??
  14. //??????????????????adapter.notifyDataSetChanged();??
  15. ????????????????????Message?msg?=?new?Message();??
  16. ????????????????????msg.what?=?0;??
  17. ????????????????????handler.sendMessage(msg);??
  18. ????????????????}??
  19. ????????????}??
  20. ????????}??
  21. ????}??


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


9、源代码

  相关解决方案