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

来源:互联网 发布:英伟达驱动游戏优化 编辑:程序博客网 时间:2024/05/21 17:44

具体内容如下效果图:


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <RelativeLayout        android:id="@+id/plugin_camera_image_folder_headview"        android:layout_width="fill_parent"        android:layout_height="45dp"        android:layout_marginBottom="3dp"        android:background="#2B4058"        android:gravity="center_vertical" >        <TextView            android:id="@+id/chat_publish_complete_cancle"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:layout_centerVertical="true"            android:layout_marginLeft="10dp"            android:text="取消"            android:textColor="#ffffff"            android:textSize="16sp" />        <TextView            android:id="@+id/chat_publish_complete_title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:text="选择当前位置"            android:textColor="#ffffff"            android:textSize="20sp" />        <Button            android:id="@+id/chat_publish_complete_publish"            android:layout_width="55dp"            android:layout_height="27dp"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            android:layout_marginRight="10dp"            android:background="@drawable/chat_publish_bg"            android:text="完成"            android:textColor="#fff"            android:textSize="16sp" />    </RelativeLayout>    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="2" >        <com.baidu.mapapi.map.MapView            android:id="@+id/bmapView"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:clickable="true" />        <Button            android:id="@+id/request"            android:layout_width="40dp"            android:layout_height="40dp"            android:layout_alignParentBottom="true"            android:layout_alignParentLeft="true"            android:layout_marginBottom="40dp"            android:layout_marginLeft="10dp"            android:background="@drawable/custom_loc" />    </RelativeLayout>    <ListView        android:id="@+id/lv_location_nearby"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="3" /></LinearLayout>
2、初始化控件

<span style="white-space:pre"></span>dataList = new ArrayList<PoiInfo>();mMapView = (MapView) findViewById(R.id.bmapView);mCompleteButton = (Button) findViewById(R.id.chat_publish_complete_publish);mRequestLocation = (Button) findViewById(R.id.request);mListView = (ListView) findViewById(R.id.lv_location_nearby);checkPosition=0;adapter = new ListAdapter(0);mListView.setAdapter(adapter);

3、 定位

<span style="white-space:pre"></span>//重新设置checkPosition = 0;adapter.setCheckposition(0);mBaiduMap = mMapView.getMap();mBaiduMap.clear();// 开启定位图层mBaiduMap.setMyLocationEnabled(true);mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(17).build()));   // 设置级别// 定位初始化mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类mLocationClient.registerLocationListener(myListener);// 注册定位监听接口/** * 设置定位参数 */LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式//option.setScanSpan(5000);// 设置发起定位请求的间隔时间,msoption.setNeedDeviceDirect(true);// 设置返回结果包含手机的方向option.setOpenGps(true);option.setAddrType("all");// 返回的定位结果包含地址信息option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02option.setIsNeedAddress(true);// 返回的定位结果包含地址信息mLocationClient.setLocOption(option);mLocationClient.start(); // 调用此方法开始定位
4、定位SDK监听函数

public class MyLocationListener implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null || mMapView == null) {return;}locType = location.getLocType();Log.i("mybaidumap", "当前定位的返回值是:"+locType);longitude = location.getLongitude();latitude = location.getLatitude();if (location.hasRadius()) {// 判断是否有定位精度半径radius = location.getRadius();}if (locType == BDLocation.TypeNetWorkLocation) {addrStr = location.getAddrStr();// 获取反地理编码(文字描述的地址)Log.i("mybaidumap", "当前定位的地址是:"+addrStr);}direction = location.getDirection();// 获取手机方向,【0~360°】,手机上面正面朝北为0°province = location.getProvince();// 省份city = location.getCity();// 城市district = location.getDistrict();// 区县LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());//将当前位置加入List里面PoiInfo info = new PoiInfo();info.address = location.getAddrStr();info.city = location.getCity();info.location = ll;info.name = location.getAddrStr();dataList.add(info);adapter.notifyDataSetChanged();Log.i("mybaidumap", "province是:"+province +" city是"+city +" 区县是: "+district);// 构造定位数据MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())// 此处设置开发者获取到的方向信息,顺时针0-360.direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();mBaiduMap.setMyLocationData(locData);//画标志CoordinateConverter converter = new CoordinateConverter();    converter.coord(ll);    converter.from(CoordinateConverter.CoordType.COMMON);    LatLng convertLatLng = converter.convert();        OverlayOptions ooA = new MarkerOptions().position(ll).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka));    mCurrentMarker = (Marker) mBaiduMap.addOverlay(ooA);              MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 17.0f);     mBaiduMap.animateMapStatus(u);          //画当前定位标志     MapStatusUpdate uc = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(uc); mMapView.showZoomControls(false); //poi 搜索周边 new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubLooper.prepare(); searchNeayBy();Looper.loop();}}).start(); }

5、搜索周边:

private void searchNeayBy(){// POI初始化搜索模块,注册搜索事件监听mPoiSearch = PoiSearch.newInstance();mPoiSearch.setOnGetPoiSearchResultListener(this);PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption();poiNearbySearchOption.keyword("公司");poiNearbySearchOption.location(new LatLng(latitude, longitude));poiNearbySearchOption.radius(100);  // 检索半径,单位是米poiNearbySearchOption.pageCapacity(20);  // 默认每页10条mPoiSearch.searchNearby(poiNearbySearchOption);  // 发起附近检索请求}
7、周边地理位置结果返回

@Overridepublic void onGetPoiResult(PoiResult result) {// 获取POI检索结果if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {// 没有找到检索结果Toast.makeText(MainActivity.this, "未找到结果",Toast.LENGTH_LONG).show();return;}if (result.error == SearchResult.ERRORNO.NO_ERROR) {// 检索结果正常返回//mBaiduMap.clear();if(result != null){if(result.getAllPoi()!= null && result.getAllPoi().size()>0){dataList.addAll(result.getAllPoi());//adapter.notifyDataSetChanged();Message msg = new Message();        msg.what = 0;        handler.sendMessage(msg);}}}}

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


9、源代码


3 0
原创粉丝点击