android 百度地图定位辅助教程
這里使用講解的是androidStudio下開發android應用,實現百度地圖定位功能的輔助教程。
登入百度地圖API官方網站http://lbsyun.baidu.com/
在主頁中選擇開發->Android定位sdk,我們可以看到有很多的教程,包括教你獲取秘鑰,開發指南等等。具體我就不再贅述了,可以看上面的開發指南,這篇文章主要針對app中加入百度定位的一些細節方面的操作。
接著選擇相關下載->全部下載,進入之后可以看到如下界面。我們這里只勾選全量定位即可。
點擊下載開發包,當然你也可以下載示例代碼和參考類。
下載好開發包之后可以解壓看到如下目錄結構:
有可能你會問這些是干嘛用的,其實這些都是動態鏈接庫,針對不同架構的android手機cpu百度編寫了不同的庫文件,目的是為了適配不同的CPU架構,如果你選擇了多個功能的開發包,會發現非常大,這時我們就得取舍,我們只要留下armeabi和 armeabi-v7a 這兩文件的庫就行了,基本上市面機子都ok。
知識提示:
armeabi就是針對普通的或舊的arm v5 cpu,armeabi-v7a是針對有浮點運算或高級擴展功能的arm v7 cpu。
armeabi-v7a(32位ARM設備),arm64-v8a(64位ARM設備)。
對于.so文件放哪,你可能會感到困惑,如下總結:
- AndroidStudio工程放在jniLibs/ABI目錄中(當然也可以通過在build.gradle文件中的設置jniLibs.srcDir屬性自己指定);
- Eclipse工程放在libs/ABI目錄中(這也是ndk-build命令默認生成.so文件的目錄);
- AAR壓縮包中位于jni/ABI目錄中(.so文件會自動包含到引用AAR壓縮包的APK中);
- 最終在APK文件中的lib/ABI目錄中
通過PackageManager安裝后,在小于Android 5.0的系統中,.so文件位于app的nativeLibraryPath目錄中;在大于等于Android 5.0的系統中,.so文件位于app的nativeLibraryRootDir/CPU_ARCH目錄中。
接下來是將這些庫文件以及jar包導入到android工程中,如下操作,這里就將百度地圖的動態鏈接庫全部導入到應用中吧,全部復制粘貼到libs目錄下:
將jar包選擇右鍵,Add As Library,這樣成功將jar包導入,圖中沒有顯示庫文件,是因為樓主截圖不是同一個工程導致。正常將會顯示出庫文件,請諒解。
當然你也可以通過Project Structure的方式將jar包導入,如下操作:
完成jar包的導入,可以看到在android工程的Gradle(Module:app)文件中看到如下的描述,這意味著,您的jar包導入成功了。
下面是導入后的libs目錄結構:
由于百度地圖定位需要用到動態鏈接庫,我們需要在Gradle(Module:app)文件中添加如下代碼:
sourceSets {main {jniLibs.srcDirs = ['libs']}}還需要在Manifests文件中聲明服務以及配置秘鑰,如下:
對了,別忘了還要聲明一些權限,詳情請看官方開發指南
好了這樣百度地圖定位的準備工作就基本做好了。現在你就可以看百度的圖的開發指南,進行代碼的編寫
官方開發指南地址:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/getloc
作者在做自己項目時寫了個工具類,方便操作:
public class BDLocationUtils implements BDLocationListener {private static BDLocationUtils bdLocationUtils;public LocationClient mLocationClient = null;private String describe = "";private List<Poi> locationPoiList = new ArrayList<>();private List<String> locationList = new ArrayList<>();private String address;public static BDLocationUtils newInstance(Context context) {if (bdLocationUtils == null) {bdLocationUtils = new BDLocationUtils(context);}return bdLocationUtils;}public BDLocationUtils(Context context) {System.out.println("bdlocation");mLocationClient = new LocationClient(context); //聲明LocationClient類initLocation();mLocationClient.registerLocationListener(this); //注冊監聽函數mLocationClient.start();}/*** 初始化位置獲取設置*/private void initLocation() {LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可選,默認高精度,設置定位模式,高精度,低功耗,僅設備option.setCoorType("bd09ll");//可選,默認gcj02;設置返回的定位結果坐標系// int span = 1000;//option.setScanSpan(span);//可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大于等于1000ms才是有效的option.setIsNeedAddress(true);//可選,設置是否需要地址信息,默認不需要option.setOpenGps(false);//可選,默認false,設置是否使用gpsoption.setLocationNotify(false);//可選,默認false,設置是否當gps有效時按照1S1次頻率輸出GPS結果option.setIsNeedLocationDescribe(false);//可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe里得到,結果類似于“在北京天安門附近”option.setIsNeedLocationPoiList(true);//可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可選,默認true,定位SDK內部是一個SERVICE,并放到了獨立進程,設置是否在stop的時候殺死這個進程,默認不殺死option.SetIgnoreCacheException(false);//可選,默認false,設置是否收集CRASH信息,默認收集option.setEnableSimulateGps(true);//可選,默認false,設置是否需要過濾gps仿真結果,默認需要mLocationClient.setLocOption(option);}@Overridepublic void onReceiveLocation(BDLocation location) {System.out.println("bdlocation listener。");//Receive LocationStringBuffer 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());//latitude = location.getLatitude();MyApplication.location.setLatitude(location.getLatitude());//緯度sb.append("\nlontitude : ");sb.append(location.getLongitude());//經度MyApplication.location.setLatitude(location.getLongitude());//longitude = location.getLongitude(); // sb.append("\nradius : "); // sb.append(location.getRadius());if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位結果 // 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());address = location.getAddrStr();MyApplication.location.setAddress(address);sb.append(address);//Log.i("address:", address);sb.append("\ndescribe : ");sb.append("gps定位成功");describe = "gps定位成功";} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 網絡定位結果sb.append("\naddr : ");address = location.getAddrStr();sb.append(address);MyApplication.location.setAddress(address);//運營商信息sb.append("\noperationers : ");sb.append(location.getOperators());sb.append("\ndescribe : ");sb.append("網絡定位成功");describe = "網絡定位成功";} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 離線定位結果sb.append("\ndescribe : ");sb.append("離線定位成功,離線定位結果也是有效的");describe = "離線定位成功,離線定位結果也是有效的";} else if (location.getLocType() == BDLocation.TypeServerError) {sb.append("\ndescribe : ");sb.append("服務端網絡定位失敗,可以反饋IMEI號和大體定位時間到loc-bugs@baidu.com,會有人追查原因");describe = "服務端網絡定位失敗";} else if (location.getLocType() == BDLocation.TypeNetWorkException) {sb.append("\ndescribe : ");sb.append("網絡不同導致定位失敗,請檢查網絡是否通暢");describe = "服務端網絡定位失敗";} else if (location.getLocType() == BDLocation.TypeCriteriaException) {sb.append("\ndescribe : ");sb.append("無法獲取有效定位依據導致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機");describe = "無法獲取有效定位依據導致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機";}sb.append("\nlocationdescribe : ");sb.append(location.getLocationDescribe());// 位置語義化信息locationPoiList = location.getPoiList();// POI數據if (locationPoiList != null) {sb.append("\npoilist size = : ");sb.append(locationPoiList.size());locationList.clear();for (Poi p : locationPoiList) {sb.append("\npoi= : ");sb.append(p.getId() + " " + p.getName() + " " + p.getRank());locationList.add(p.getName());}//通知list數據更新}//提示定位結果Log.i("BaiduLocationApiDem", sb.toString());}public void startLocation() {mLocationClient.start();}public void stopLocation() {mLocationClient.stop();}public List<String> getLocationList() {return locationList;}public String getAddress() {return address;}public String getDescribe() {return describe;} }獲取您當前位置可能的地點:
bdLocationUtils = BDLocationUtils.newInstance(getApplicationContext()); bdLocationUtils.startLocation(); locationList = bdLocationUtils.getLocationList();locationList存放著可能得地點
下面是獲取您當前位置的地點,屬于一個范圍地點,但不是具體的
當你定位失敗的時候,你肯定想有提示一下,那么可以調用如下代碼
String describe = bdLocationUtils.getDescribe();這樣,describe就有你想要的信息。
注意,在你的activity中,finish時別忘了調用一下
以防止內存泄露。
如有問題,請留下您的腳印謝謝。
、
總結
以上是生活随笔為你收集整理的android 百度地图定位辅助教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go错误处理 11
- 下一篇: 商用密码产品(密码模块)-密码模块接口