集成百度地圖API實現定位
一.百度地圖API獲取定位
AndroidManifest.xml必須配置:
定位服務 |
<service android:name="com.baidu.location.f" android:enabled="true" android:process android:exported="false"> <intent-filter > <action android:name="com.baidu.location.service" android:exported="false"/> </intent-filter> </service> |
Key配置 |
<!-- 用於配置key --> < android:name="com.baidu.lbsapi.API_KEY" android:value="Kn1xqiWftDyndXhMqLTG2k1G" /> |
AndroidManifest.xml |
獲取地理位置--1.獲得地理編碼---2通過地理編碼獲得地理位置名稱
初始化地圖 |
// 初始化百度地圖 bMapLocation = new GetMyLocation(MainClient.this); bMapLocation.initGeoCode(); |
定位初始化 |
//定位初始化 mLocClient locData = new LocationData(); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);//打開gps option.setCoorType("bd09ll");//設置坐標類型 option.setScanSpan(10000); mLocClient.setLocOption(option); mLocClient.start(); |
通過地理編碼獲得地區名稱 |
mSearch = new MKSearch(); mSearch.init(mapManager, new MKSearchListener() {
public void onGetAddrResult(MKAddrInfo res, int error) { if (error != 0) { String str = String.format("錯誤號:%d", error); Toast.makeText(GetMyLocation.context,str, Toast.LENGTH_LONG).show(); return; } if (res.type == MKAddrInfo.MK_REVERSEGEOCODE){ //反地理編碼:通過坐標點檢索具體地址及周邊poi localname = res.strAddr; } } 。 。 。 。 。 。 |
集成百度地圖API實現定位