android使用百度地圖SDK獲取定位信息示例
本文使用Android Studio開(kāi)發(fā)。
獲取定位信息相對(duì)簡(jiǎn)單,我們只需要如下幾步:
第一步,注冊(cè)百度賬號(hào),在百度地圖開(kāi)放平臺(tái)新建應(yīng)用、生成API_KEY。這些就不細(xì)說(shuō)了,請(qǐng)前往這里: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工程(略過(guò)不說(shuō)),配置環(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="你申請(qǐng)的API key" /> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" />
第六步,測(cè)試代碼,獲取定位信息:
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); //注冊(cè)監(jiān)聽(tīng)函數(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ā)起定位請(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í)按照1S/1次頻率輸出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)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è)置是否需要過(guò)濾GPS仿真結(jié)果,默認(rèn)需要 mLocationClient.setLocOption(option); //開(kāi)啟定位 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)營(yíng)商信息 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號(hào)和大體定位時(shí)間到loc-bugs@baidu.com,會(huì)有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("\ndescribe : "); sb.append("網(wǎng)絡(luò)不同導(dǎo)致定位失敗,請(qǐng)檢查網(wǎng)絡(luò)是否通暢"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("\ndescribe : "); sb.append("無(wú)法獲取有效定位依據(jù)導(dǎo)致定位失敗,一般是由于手機(jī)的原因,處于飛行模式下一般會(huì)造成這種結(jié)果,可以試著重啟手機(jī)"); } sb.append("\nlocationdescribe : "); sb.append(location.getLocationDescribe());// 位置語(yǔ)義化信息 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()); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中ImageView的ScaleType使用方法
這篇文章主要介紹了Android 中ImageView的ScaleType使用方法的相關(guān)資料,希望通過(guò)本能幫助到大家,需要的朋友可以參考下2017-09-09Android控件實(shí)現(xiàn)直播App點(diǎn)贊飄心動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了FlowLikeView控件實(shí)現(xiàn)直播App特效之點(diǎn)贊飄心動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Kotlin 匿名類實(shí)現(xiàn)接口和抽象類的區(qū)別詳解
這篇文章主要介紹了Kotlin 匿名類實(shí)現(xiàn)接口和抽象類的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03RecyclerView進(jìn)階:使用ItemTouchHelper實(shí)現(xiàn)拖拽和側(cè)滑刪除效果
現(xiàn)在RecyclerView的應(yīng)用越來(lái)越廣泛了,本篇文章主要介紹了RecyclerView進(jìn)階:使用ItemTouchHelper實(shí)現(xiàn)拖拽和側(cè)滑刪除效果,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView
這篇文章主要介紹了Android實(shí)現(xiàn)樹(shù)形層級(jí)ListView的相關(guān)資料,需要的朋友可以參考下2016-02-02Android筆記之:App自動(dòng)化之使用Ant編譯項(xiàng)目多渠道打包的使用詳解
本篇文章介紹了,Android筆記之:App自動(dòng)化之使用Ant編譯項(xiàng)目多渠道打包的使用詳解。需要的朋友參考下2013-04-04android開(kāi)發(fā)PathEffect問(wèn)題處理
本文主要整理了關(guān)于android中PathEffect的問(wèn)題匯總以及處理方式,以及給大家做了關(guān)于PathEffect類的詳細(xì)解釋。2017-11-11