android使用百度地圖SDK獲取定位信息示例
本文使用Android Studio開發(fā)。
獲取定位信息相對簡單,我們只需要如下幾步:
第一步,注冊百度賬號,在百度地圖開放平臺新建應(yīng)用、生成API_KEY。這些就不細(xì)說了,請前往這里:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/key
第二步,下載sdk,地址:http://lbsyun.baidu.com/index.php?title=android-locsdk/geosdk-android-download
第三步,建立Android Studio工程(略過不說),配置環(huán)境:
將解壓后的文件放入libs文件夾下,并在src/main下建立一個(gè)叫做jniLibs的文件夾,并把解壓后內(nèi)的文件夾靠進(jìn)去,如下圖:

第四步,將BaiduLBS_Android.jar加入環(huán)境變量(右鍵,Add As Library),并在app的build.gradle中的android中添加:
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
如圖:

第五步,在AndroidManifest.xml文件中聲明權(quán)限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--網(wǎng)絡(luò)定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!--GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
并在application標(biāo)簽中添加如下內(nèi)容:
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="你申請的API key" />
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" />
第六步,測試代碼,獲取定位信息:
public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLocate();
}
/**
* 定位
*/
private void startLocate() {
mLocationClient = new LocationClient(getApplicationContext()); //聲明LocationClient類
mLocationClient.registerLocationListener(myListener); //注冊監(jiān)聽函數(shù)
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving
);//可選,默認(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ā)起定位請求的間隔需要大于等于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í)按照1S/1次頻率輸出GPS結(jié)果
option.setIsNeedLocationDescribe(true);//可選,默認(rèn)false,設(shè)置是否需要位置語義化結(jié)果,可以在BDLocation.getLocationDescribe里得到,結(jié)果類似于“在北京天安門附近”
option.setIsNeedLocationPoiList(true);//可選,默認(rèn)false,設(shè)置是否需要POI結(jié)果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);//可選,默認(rèn)true,定位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è)置是否需要過濾GPS仿真結(jié)果,默認(rèn)需要
mLocationClient.setLocOption(option);
//開啟定位
mLocationClient.start();
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位結(jié)果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 單位:公里每小時(shí)
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 單位:米
sb.append("\ndirection : ");
sb.append(location.getDirection());// 單位度
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 網(wǎng)絡(luò)定位結(jié)果
sb.append("\naddr : ");
sb.append(location.getAddrStr());
//運(yùn)營商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("網(wǎng)絡(luò)定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 離線定位結(jié)果
sb.append("\ndescribe : ");
sb.append("離線定位成功,離線定位結(jié)果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服務(wù)端網(wǎng)絡(luò)定位失敗,可以反饋IMEI號和大體定位時(shí)間到loc-bugs@baidu.com,會有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("網(wǎng)絡(luò)不同導(dǎo)致定位失敗,請檢查網(wǎng)絡(luò)是否通暢");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("無法獲取有效定位依據(jù)導(dǎo)致定位失敗,一般是由于手機(jī)的原因,處于飛行模式下一般會造成這種結(jié)果,可以試著重啟手機(jī)");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe());// 位置語義化信息
List<Poi> list = location.getPoiList();// POI數(shù)據(jù)
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
}
}
Log.e("描述:", sb.toString());
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View之組合控件實(shí)現(xiàn)類似電商app頂部欄
這篇文章主要為大家詳細(xì)介紹了Android自定義View之組合控件,實(shí)現(xiàn)類似電商app頂部欄的相關(guān)資料,具有參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android音視頻開發(fā)只硬件解碼組件MediaCodec講解
在Android開發(fā)中提供了實(shí)現(xiàn)音視頻編解碼工具M(jìn)ediaCodec,針對對應(yīng)音視頻解碼類型通過該類創(chuàng)建對應(yīng)解碼器就能實(shí)現(xiàn)對數(shù)據(jù)進(jìn)行解碼操作。本文通過示例詳細(xì)講解了MediaCodec的使用,需要的可以參考一下2023-01-01
Kotlin HttpURLConnection與服務(wù)器交互實(shí)現(xiàn)方法詳解
簡單來說,HttpURLConnection 是發(fā)起HTTP請求的基礎(chǔ)類庫,提供了HTTP請求的基本功能,不過封裝的比較少,在使用時(shí)很多內(nèi)容都需要自己設(shè)置,也需要自己處理請求流和響應(yīng)流2022-09-09
使用OkHttp包在Android中進(jìn)行HTTP頭處理的教程
HTTP頭部處理是HTTP網(wǎng)絡(luò)編程中的基本操作,安卓中使用OkHttp包(github.com/square/okhttp)進(jìn)行相關(guān)操作當(dāng)然也是得心應(yīng)手,這里我們就來看一下使用OkHttp包在Android中進(jìn)行HTTP頭處理的教程2016-07-07
Android實(shí)現(xiàn)原生鎖屏頁面音樂控制
這篇文章主要介紹了Android實(shí)現(xiàn)原生鎖屏頁面音樂控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12

