Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼
本文實(shí)例講解Android百度地圖定位后獲取周邊位置的實(shí)現(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>

布局文件就是上面是一個(gè)百度地圖的mapview,下面是一個(gè)顯示周邊位置的ListView,很簡(jiǎn)單。
1、自動(dòng)定位
我們先看一下根據(jù)自己的地理位置實(shí)現(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 ); //注冊(cè)監(jiān)聽函數(shù)
}
3.配置定位SDK參數(shù)
設(shè)置定位參數(shù)包括:定位模式(高精度定位模式,低功耗定位模式和僅用設(shè)備定位模式),返回坐標(biāo)類型,是否打開GPS,是否返回地址信息、位置語(yǔ)義化信息、POI信息等等。
LocationClientOption類,該類用來(lái)設(shè)置定位SDK的定位方式
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy
);//可選,默認(rèn)高精度,設(shè)置定位模式,高精度,低功耗,僅設(shè)備
option.setCoorType("bd09ll");//可選,默認(rèn)gcj02,設(shè)置返回的定位結(jié)果坐標(biāo)系
int span=1000;
option.setScanSpan(span);//可選,默認(rèn)0,即僅定位一次,設(shè)置發(fā)起定位請(qǐng)求的間隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);//可選,設(shè)置是否需要地址信息,默認(rèn)不需要
option.setOpenGps(true);//可選,默認(rèn)false,設(shè)置是否使用gps
option.setLocationNotify(true);//可選,默認(rèn)false,設(shè)置是否當(dāng)gps有效時(shí)按照1S1次頻率輸出GPS結(jié)果
option.setIsNeedLocationDescribe(true);//可選,默認(rèn)false,設(shè)置是否需要位置語(yǔ)義化結(jié)果,可以在BDLocation.getLocationDescribe里得到,結(jié)果類似于“在北京天安門附近”
option.setIsNeedLocationPoiList(true);//可選,默認(rèn)false,設(shè)置是否需要POI結(jié)果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);//可選,默認(rèn)false,定位SDK內(nèi)部是一個(gè)SERVICE,并放到了獨(dú)立進(jìn)程,設(shè)置是否在stop的時(shí)候殺死這個(gè)進(jìn)程,默認(rèn)殺死
option.SetIgnoreCacheException(false);//可選,默認(rèn)false,設(shè)置是否收集CRASH信息,默認(rèn)收集
option.setEnableSimulateGps(false);//可選,默認(rèn)false,設(shè)置是否需要過(guò)濾gps仿真結(jié)果,默認(rèn)需要
mLocationClient.setLocOption(option);
}
4.實(shí)現(xiàn)BDLocationListener接口
/**
* 監(jiān)聽函數(shù),有新位置的時(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ù),相信總有一個(gè)對(duì)你有用的。
2、根據(jù)經(jīng)緯度定位
這種方法不需要自動(dòng)定位,就是根據(jù)經(jīng)緯度來(lái)顯示地圖上的位置
/*
* 顯示經(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中的一個(gè)類PoiNearbySearchOption,我們可以看一下類參考:
- PoiNearbySearchOption keyword(java.lang.String key)
- 檢索關(guān)鍵字
- PoiNearbySearchOption location(LatLng location)
- 檢索位置
- PoiNearbySearchOption pageCapacity(int pageCapacity)
- 設(shè)置每頁(yè)容量,默認(rèn)為每頁(yè)10條
- PoiNearbySearchOption pageNum(int pageNum)
- 分頁(yè)編號(hào)
- PoiNearbySearchOption radius(int radius)
- 設(shè)置檢索的半徑范圍
- PoiNearbySearchOption sortType(PoiSortType sortType)
- 搜索結(jié)果排序規(guī)則,可選,默認(rèn)
這里是它的一些方法,我們可以看到我們只需要設(shè)置一下關(guān)鍵字、周邊位置半徑、檢索位置、排序規(guī)則、分頁(yè)號(hào)、每頁(yè)數(shù)量等。然后我們實(shí)現(xiàn)OnGetPoiSearchResultListener這個(gè)接口,獲取周邊地理位置結(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了,最后再實(shí)現(xiàn)一個(gè)小小的功能,就是點(diǎn)擊列表中的每個(gè)位置,顯示位置的小圖標(biāo)根據(jù)位置的改變而改變
/**
* 周邊地理位置列表點(diǎn)擊事件
*/
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);
}
}
});
好了,很簡(jiǎn)單有用的一個(gè)小功能,會(huì)給用戶帶來(lái)很好的體驗(yàn)效果。
希望大家會(huì)喜歡這篇文章。
相關(guān)文章
一個(gè)簡(jiǎn)單的toolabar結(jié)合drawlayout使用方法
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的toolabar結(jié)合drawlayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼
這篇文章主要介紹了Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Flutter?隊(duì)列任務(wù)的實(shí)現(xiàn)
本文主要介紹了Flutter?隊(duì)列任務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Qt6.5.3?Android環(huán)境配置的實(shí)現(xiàn)
本文主要介紹了Qt6.5.3?Android環(huán)境配置的實(shí)現(xiàn),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Android選擇圖片或視頻進(jìn)行循環(huán)播放
這篇文章主要為大家詳細(xì)介紹了Android選擇圖片或視頻進(jìn)行循環(huán)播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android中網(wǎng)絡(luò)框架簡(jiǎn)單封裝的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于Android中網(wǎng)絡(luò)框架簡(jiǎn)單封裝的實(shí)例方法,需要的朋友們可以學(xué)習(xí)下。2020-03-03
Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法示例
這篇文章主要介紹了Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法,結(jié)合實(shí)例形式分析了Android滑動(dòng)效果的原理及擴(kuò)展ViewGroup實(shí)現(xiàn)滑動(dòng)功能的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

