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下建立一個叫做jniLibs的文件夾,并把解壓后內(nèi)的文件夾靠進去,如下圖:
第四步,將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有效時按照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)部是一個SERVICE,并放到了獨立進程,設(shè)置是否在stop的時候殺死這個進程,默認(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());// 單位:公里每小時 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()); //運營商信息 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號和大體定位時間到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)致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結(jié)果,可以試著重啟手機"); } 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 中ImageView的ScaleType使用方法
這篇文章主要介紹了Android 中ImageView的ScaleType使用方法的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下2017-09-09Kotlin 匿名類實現(xiàn)接口和抽象類的區(qū)別詳解
這篇文章主要介紹了Kotlin 匿名類實現(xiàn)接口和抽象類的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03RecyclerView進階:使用ItemTouchHelper實現(xiàn)拖拽和側(cè)滑刪除效果
現(xiàn)在RecyclerView的應(yīng)用越來越廣泛了,本篇文章主要介紹了RecyclerView進階:使用ItemTouchHelper實現(xiàn)拖拽和側(cè)滑刪除效果,具有一定的參考價值,有興趣的可以了解一下。2017-02-02Android筆記之:App自動化之使用Ant編譯項目多渠道打包的使用詳解
本篇文章介紹了,Android筆記之:App自動化之使用Ant編譯項目多渠道打包的使用詳解。需要的朋友參考下2013-04-04