
具体代码:
1.布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="@dimen/height_top_bar"android:background="@color/common_top_bar_dark"android:gravity="center_vertical"><Buttonandroid:id="@+id/btn_location_back"android:layout_width="wrap_content"android:layout_height="match_parent"android:drawableLeft="@drawable/back"android:text="@string/top_back"style="@style/btn_title_bar"android:layout_alignParentLeft="true"android:onClick="back"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="@string/location_message"style="@style/txt_titlebar_message"/><Buttonandroid:id="@+id/btn_location_ok"android:layout_width="52dp"android:layout_height="match_parent"android:layout_alignParentRight="true"android:background="@drawable/common_tab_bg"android:text="@string/txt_queding"style="@style/btn_title_bar"/></RelativeLayout><com.baidu.mapapi.map.MapViewandroid:layout_weight="2"android:id="@+id/mapview_location"android:layout_width="fill_parent"android:layout_height="match_parent"android:clickable="true" /><ListViewandroid:layout_weight="3"android:id="@+id/lv_location_nearby"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

布局文件就是上面是一个百度地图的mapview,下面是一个显示周边位置的ListView,很简单。
1、自动定位
我们先看一下根据自己的地理位置实现定位
1.首先初始化要用到的组件
/** * 初始化组件 */private void initView() {btnLocationBack = (Button) findViewById(R.id.btn_location_back);btnLocationBack.setOnClickListener(this);btnLocationOk = (Button) findViewById(R.id.btn_location_ok);btnLocationOk.setOnClickListener(this);mapViewLocation = (MapView) findViewById(R.id.mapview_location);lvLocNear = (ListView) findViewById(R.id.lv_location_nearby);nearList = new ArrayList<PoiInfo>();adapter = new LocNearAddressAdapter(context, nearList, isSelected);lvLocNear.setAdapter(adapter);}2.初始化LocationClient类,该类需要在主线程中声明public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();public void onCreate() {mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类mLocationClient.registerLocationListener( myListener );//注册监听函数}3.配置定位SDK参数 private void initLocation(){LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系int span=1000;option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要option.setOpenGps(true);//可选,默认false,设置是否使用gpsoption.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可选,默认false,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认杀死option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要mLocationClient.setLocOption(option);}4.实现BDLocationListener接口 /** * 监听函数,有新位置的时候,格式化成字符串,输出到屏幕中 */public class MyLocationListenner implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null) {return;}Log.d("map", "On location change received:" + location);Log.d("map", "addr:" + location.getAddrStr());if (progressDialog != null) {progressDialog.dismiss();}if (lastLocation != null) {if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {Log.d("map", "same location, skip refresh");// mMapView.refresh(); //need this refresh?return;}}lastLocation = location;mBaiduMap.clear();mCurrentLantitude = lastLocation.getLatitude();mCurrentLongitude = lastLocation.getLongitude();Log.e(">>>>>>>", mCurrentLantitude + "," + mCurrentLongitude);LatLng llA = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());CoordinateConverter converter = new CoordinateConverter();converter.coord(llA);converter.from(CoordinateConverter.CoordType.COMMON);LatLng convertLatLng = converter.convert();OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka)).zIndex(4).draggable(true);mCurrentMarker = (Marker) mBaiduMap.addOverlay(ooA);MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 16.0f);mBaiduMap.animateMapStatus(u);new Thread(new Runnable() {@Overridepublic void run() {searchNeayBy();}}).start();}public void onReceivePoi(BDLocation poiLocation) {if (poiLocation == null) {return;}}}这里接受到的BDLocation中包含好多参数,相信总有一个对你有用的。 /** 显示经纬度的位置* */private void showMap(double latitude, double longtitude, String address) {//sendButton.setVisibility(View.GONE);LatLng llA = new LatLng(latitude, longtitude);CoordinateConverter converter = new CoordinateConverter();converter.coord(llA);converter.from(CoordinateConverter.CoordType.COMMON);LatLng convertLatLng = converter.convert();OverlayOptions ooA = new MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka)).zIndex(4).draggable(true);markerA = (Marker) (mBaiduMap.addOverlay(ooA));u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng, 16.0f);mBaiduMap.animateMapStatus(u);new Thread(new Runnable() {@Overridepublic void run() {searchNeayBy();}}).start();}3、获取周边地理位置/** * 搜索周边地理位置 */private void searchNeayBy() {PoiNearbySearchOption option = new PoiNearbySearchOption();option.keyword("写字楼");option.sortType(PoiSortType.distance_from_near_to_far);option.location(new LatLng(mCurrentLantitude, mCurrentLongitude));if (radius != 0) {option.radius(radius);} else {option.radius(1000);}option.pageCapacity(20);mPoiSearch.searchNearby(option);}/** 接受周边地理位置结果* @param poiResult*/@Overridepublic void onGetPoiResult(PoiResult poiResult) {if (poiResult != null) {if (poiResult.getAllPoi()!=null&&poiResult.getAllPoi().size()>0){nearList.addAll(poiResult.getAllPoi());if (nearList != null && nearList.size() > 0) {for (int i = 0; i < nearList.size(); i++) {isSelected.put(i, false);}}Message msg = new Message();msg.what = 0;handler.sendMessage(msg);}}}获取完数据之后更新适配器显示周边位置就OK了,最后再实现一个小小的功能,就是点击列表中的每个位置,显示位置的小图标根据位置的改变而改变 /** * 周边地理位置列表点击事件 */lvLocNear.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {adapter.setSelected(i);adapter.notifyDataSetChanged();PoiInfo ad = (PoiInfo) adapter.getItem(i);u = MapStatusUpdateFactory.newLatLng(ad.location);mBaiduMap.animateMapStatus(u);if (!isLoc) {mCurrentMarker.setPosition(ad.location);} else {markerA.setPosition(ad.location);}}});好了,很简单有用的一个小功能,会给用户带来很好的体验效果。