Android百度地圖定位后獲取周邊位置的實現(xiàn)代碼
本文實例講解Android百度地圖定位后獲取周邊位置的實現(xiàn)代碼,分享給大家供大家參考,具體內(nèi)容如下
效果圖:
具體代碼:
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"> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/height_top_bar" android:background="@color/common_top_bar_dark" android:gravity="center_vertical"> <Button android: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" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/location_message" style="@style/txt_titlebar_message"/> <Button android: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.MapView android:layout_weight="2" android:id="@+id/mapview_location" android:layout_width="fill_parent" android:layout_height="match_parent" android:clickable="true" /> <ListView android:layout_weight="3" android:id="@+id/lv_location_nearby" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
布局文件就是上面是一個百度地圖的mapview,下面是一個顯示周邊位置的ListView,很簡單。
1、自動定位
我們先看一下根據(jù)自己的地理位置實現(xiàn)定位
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 ); //注冊監(jiān)聽函數(shù) }
3.配置定位SDK參數(shù)
設(shè)置定位參數(shù)包括:定位模式(高精度定位模式,低功耗定位模式和僅用設(shè)備定位模式),返回坐標類型,是否打開GPS,是否返回地址信息、位置語義化信息、POI信息等等。
LocationClientOption類,該類用來設(shè)置定位SDK的定位方式
private void initLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy );//可選,默認高精度,設(shè)置定位模式,高精度,低功耗,僅設(shè)備 option.setCoorType("bd09ll");//可選,默認gcj02,設(shè)置返回的定位結(jié)果坐標系 int span=1000; option.setScanSpan(span);//可選,默認0,即僅定位一次,設(shè)置發(fā)起定位請求的間隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可選,設(shè)置是否需要地址信息,默認不需要 option.setOpenGps(true);//可選,默認false,設(shè)置是否使用gps option.setLocationNotify(true);//可選,默認false,設(shè)置是否當gps有效時按照1S1次頻率輸出GPS結(jié)果 option.setIsNeedLocationDescribe(true);//可選,默認false,設(shè)置是否需要位置語義化結(jié)果,可以在BDLocation.getLocationDescribe里得到,結(jié)果類似于“在北京天安門附近” option.setIsNeedLocationPoiList(true);//可選,默認false,設(shè)置是否需要POI結(jié)果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可選,默認false,定位SDK內(nèi)部是一個SERVICE,并放到了獨立進程,設(shè)置是否在stop的時候殺死這個進程,默認殺死 option.SetIgnoreCacheException(false);//可選,默認false,設(shè)置是否收集CRASH信息,默認收集 option.setEnableSimulateGps(false);//可選,默認false,設(shè)置是否需要過濾gps仿真結(jié)果,默認需要 mLocationClient.setLocOption(option); }
4.實現(xiàn)BDLocationListener接口
/** * 監(jiān)聽函數(shù),有新位置的時候,格式化成字符串,輸出到屏幕中 */ public class MyLocationListenner implements BDLocationListener { @Override public 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() { @Override public void run() { searchNeayBy(); } }).start(); } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null) { return; } } }
這里接受到的BDLocation中包含好多參數(shù),相信總有一個對你有用的。
2、根據(jù)經(jīng)緯度定位
這種方法不需要自動定位,就是根據(jù)經(jīng)緯度來顯示地圖上的位置
/* * 顯示經(jīng)緯度的位置 * */ 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() { @Override public void run() { searchNeayBy(); } }).start(); }
3、獲取周邊地理位置
最后看一下怎么獲取周邊的地理位置,這里需要用到SDK中的一個類PoiNearbySearchOption,我們可以看一下類參考:
- PoiNearbySearchOption keyword(java.lang.String key)
- 檢索關(guān)鍵字
- PoiNearbySearchOption location(LatLng location)
- 檢索位置
- PoiNearbySearchOption pageCapacity(int pageCapacity)
- 設(shè)置每頁容量,默認為每頁10條
- PoiNearbySearchOption pageNum(int pageNum)
- 分頁編號
- PoiNearbySearchOption radius(int radius)
- 設(shè)置檢索的半徑范圍
- PoiNearbySearchOption sortType(PoiSortType sortType)
- 搜索結(jié)果排序規(guī)則,可選,默認
這里是它的一些方法,我們可以看到我們只需要設(shè)置一下關(guān)鍵字、周邊位置半徑、檢索位置、排序規(guī)則、分頁號、每頁數(shù)量等。然后我們實現(xiàn)OnGetPoiSearchResultListener這個接口,獲取周邊地理位置結(jié)果。
/** * 搜索周邊地理位置 */ 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); } /* * 接受周邊地理位置結(jié)果 * @param poiResult */ @Override public 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); } } }
獲取完數(shù)據(jù)之后更新適配器顯示周邊位置就OK了,最后再實現(xiàn)一個小小的功能,就是點擊列表中的每個位置,顯示位置的小圖標根據(jù)位置的改變而改變
/** * 周邊地理位置列表點擊事件 */ lvLocNear.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public 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); } } });
好了,很簡單有用的一個小功能,會給用戶帶來很好的體驗效果。
希望大家會喜歡這篇文章。
相關(guān)文章
Android使用自定義View實現(xiàn)橫行時間軸效果
這篇文章主要給大家介紹了關(guān)于Android使用自定義View實現(xiàn)橫行時間軸效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Android具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12Android WebView或手機瀏覽器打開連接問題解決辦法總結(jié)
這篇文章主要介紹了Android WebView或手機瀏覽器打開連接問題解決辦法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03Andriod arcgis保存Mapview為圖片的實例代碼
這篇文章主要介紹了Andriod arcgis保存Mapview為圖片的實例代碼 的相關(guān)資料,需要的朋友可以參考下2016-03-03Android TextView控件文字添加下劃線的實現(xiàn)方法
下面小編就為大家?guī)硪黄狝ndroid TextView控件文字添加下劃線的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09Android實現(xiàn)判斷某個服務(wù)是否正在運行的方法
這篇文章主要介紹了Android實現(xiàn)判斷某個服務(wù)是否正在運行的方法,涉及Android針對系統(tǒng)服務(wù)運行狀態(tài)的判斷技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android實現(xiàn)從底部彈出的Dialog的實例代碼
這篇文章主要介紹了Android實現(xiàn)從底部彈出的Dialog的實例代碼,非常不錯,具有參考借鑒價值 ,需要的朋友可以參考下2018-04-04