Android 百度定位API使用方法

轉載自:http://lszdb1983.blog.163.com/blog/static/20426348201272924223933/

導入庫文件

在下載頁面下載最新的庫文件。將liblocSDK2.4.so文件拷貝到libs/armeabi目錄下。將locSDK2.4.jar文件拷貝到工程根目錄下,並在工程屬性->Java Build Path->Libraries中選擇“Add JARs”,選定locSDK2.4.jar,確定後返回。這樣您就可以在程序中使用百度定位API了。 

設置AndroidManifest.xml

爲區分2.3版本service,需要將manifest file中的 intent filter聲明爲com.baidu.location.service_v2.4 在application標籤中聲明service組件

  1. <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"
  2. android:permission="android.permission.BAIDU_LOCATION_SERVICE">
  3. <intent-filter>
  4. <action android:name="com.baidu.location.service_v2.4"></action>
  5. </intent-filter>
  6. </service>

聲明使用權限

  1. <permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
  2. <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
  4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  5. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  6. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  7. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  8. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  9. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  10. <uses-permission android:name="android.permission.INTERNET" />
  11. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
  12. <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

import相關類

  1. import com.baidu.location.BDLocation;
  2. import com.baidu.location.BDLocationListener;
  3. import com.baidu.location.LocationClient;
  4. import com.baidu.location.LocationClientOption;
  5. import com.baidu.location.BDNotifyListener;//假如用到位置提醒功能,需要import該類

功能類的使用

初始化LocationClient類

此處需要注意:LocationClient類必須在主線程中聲明。需要Context類型的參數。

  1. public LocationClient mLocationClient = null;
  2. public BDLocationListener myListener = new MyLocationListener();
  3.  
  4. public void onCreate() {
  5. mLocationClient = new LocationClient(this); //聲明LocationClient類
  6. mLocationClient.registerLocationListener( myListener ); //註冊監聽函數
  7. }

實現BDLocationListener接口

BDLocationListener接口有2個方法需要實現:
1.接收異步返回的定位結果,參數是BDLocation類型參數。
2.接收異步返回的POI查詢結果,參數是BDLocation類型參數。

  1. public class MyLocationListenner implements BDLocationListener {
  2. @Override
  3. public void onReceiveLocation(BDLocation location) {
  4. if (location == null)
  5. return ;
  6. StringBuffer sb = new StringBuffer(256);
  7. sb.append("time : ");
  8. sb.append(location.getTime());
  9. sb.append("\nerror code : ");
  10. sb.append(location.getLocType());
  11. sb.append("\nlatitude : ");
  12. sb.append(location.getLatitude());
  13. sb.append("\nlontitude : ");
  14. sb.append(location.getLongitude());
  15. sb.append("\nradius : ");
  16. sb.append(location.getRadius());
  17. if (location.getLocType() == BDLocation.TypeGpsLocation){
  18. sb.append("\nspeed : ");
  19. sb.append(location.getSpeed());
  20. sb.append("\nsatellite : ");
  21. sb.append(location.getSatelliteNumber());
  22. } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
  23. sb.append("\naddr : ");
  24. sb.append(location.getAddrStr());
  25. }
  26.  
  27. logMsg(sb.toString());
  28. }
  29. public void onReceivePoi(BDLocation poiLocation) {
  30. if (poiLocation == null){
  31. return ;
  32. }
  33. StringBuffer sb = new StringBuffer(256);
  34. sb.append("Poi time : ");
  35. sb.append(poiLocation.getTime());
  36. sb.append("\nerror code : ");
  37. sb.append(poiLocation.getLocType());
  38. sb.append("\nlatitude : ");
  39. sb.append(poiLocation.getLatitude());
  40. sb.append("\nlontitude : ");
  41. sb.append(poiLocation.getLongitude());
  42. sb.append("\nradius : ");
  43. sb.append(poiLocation.getRadius());
  44. if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
  45. sb.append("\naddr : ");
  46. sb.append(poiLocation.getAddrStr());
  47. }
  48. if(poiLocation.hasPoi()){
  49. sb.append("\nPoi:");
  50. sb.append(poiLocation.getPoi());
  51. }else{
  52. sb.append("noPoi information");
  53. }
  54. logMsg(sb.toString());
  55. }
  56. }

設置參數

設置定位參數包括:定位模式(單次定位,定時定位),返回座標類型,是否打開GPS等等。eg:

  1. LocationClientOption option = new LocationClientOption();
  2. option.setOpenGps(true);
  3. option.setAddrType("detail");
  4. option.setCoorType("gcj02");
  5. option.setScanSpan(5000);
  6. option.disableCache(true);//禁止啓用緩存定位
  7. option.setPoiNumber(5); //最多返回POI個數
  8. option.setPoiDistance(1000); //poi查詢距離
  9. option.setPoiExtraInfo(true); //是否需要POI的電話和地址等詳細信息
  10. mLocClient.setLocOption(option);

發起定位請求

發起定位請求。請求過程是異步的,定位結果在上面的監聽函數onReceiveLocation中獲取。

  1. if (mLocClient != null && mLocClient.isStarted())
  2. mLocClient.requestLocation();
  3. else
  4. Log.d("LocSDK_2.0_Demo1", "locClient is null or not started");

發起POI查詢請求

發起POI查詢請求。請求過程是異步的,定位結果在上面的監聽函數onReceivePoi中獲取。

  1. if (mLocClient != null && mLocClient.isStarted())
  2. mLocClient.requestPoi();

位置提醒使用

位置提醒最多提醒3次,3次過後將不再提醒。 假如需要再次提醒,或者要修改提醒點座標,都可通過函數SetNotifyLocation()來實現。

  1. //位置提醒相關代碼
  2. mNotifyer = new NotifyLister();
  3. mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4個參數代表要位置提醒的點的座標,具體含義依次爲:緯度,經度,距離範圍,座標系類型(gcj02,gps,bd09,bd09ll)
  4. mLocationClient.registerNotify(mNotifyer);
  5. //註冊位置提醒監聽事件後,可以通過SetNotifyLocation 來修改位置提醒設置,修改後立刻生效。

  1. //BDNotifyListner實現
  2. public class NotifyLister extends BDNotifyListener{
  3. public void onNotify(BDLocation mlocation, float distance){
  4. mVibrator01.vibrate(1000);//振動提醒已到設定位置附近
  5. }
  6. }

  1. //取消位置提醒
  2. mLocationClient.removeNotifyEvent(mNotifyer);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章