轉載自: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組件
- <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"
- android:permission="android.permission.BAIDU_LOCATION_SERVICE">
- <intent-filter>
- <action android:name="com.baidu.location.service_v2.4"></action>
- </intent-filter>
- </service>
聲明使用權限
- <permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
- <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
- <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
- <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
import相關類
- import com.baidu.location.BDLocation;
- import com.baidu.location.BDLocationListener;
- import com.baidu.location.LocationClient;
- import com.baidu.location.LocationClientOption;
- import com.baidu.location.BDNotifyListener;//假如用到位置提醒功能,需要import該類
功能類的使用
初始化LocationClient類
此處需要注意:LocationClient類必須在主線程中聲明。需要Context類型的參數。
- public LocationClient mLocationClient = null;
- public BDLocationListener myListener = new MyLocationListener();
- public void onCreate() {
- mLocationClient = new LocationClient(this); //聲明LocationClient類
- mLocationClient.registerLocationListener( myListener ); //註冊監聽函數
- }
實現BDLocationListener接口
BDLocationListener接口有2個方法需要實現:
1.接收異步返回的定位結果,參數是BDLocation類型參數。
2.接收異步返回的POI查詢結果,參數是BDLocation類型參數。
- public class MyLocationListenner implements BDLocationListener {
- @Override
- public void onReceiveLocation(BDLocation location) {
- if (location == null)
- return ;
- 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){
- sb.append("\nspeed : ");
- sb.append(location.getSpeed());
- sb.append("\nsatellite : ");
- sb.append(location.getSatelliteNumber());
- } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
- sb.append("\naddr : ");
- sb.append(location.getAddrStr());
- }
- logMsg(sb.toString());
- }
- public void onReceivePoi(BDLocation poiLocation) {
- if (poiLocation == null){
- return ;
- }
- sb.append("Poi time : ");
- sb.append(poiLocation.getTime());
- sb.append("\nerror code : ");
- sb.append(poiLocation.getLocType());
- sb.append("\nlatitude : ");
- sb.append(poiLocation.getLatitude());
- sb.append("\nlontitude : ");
- sb.append(poiLocation.getLongitude());
- sb.append("\nradius : ");
- sb.append(poiLocation.getRadius());
- if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
- sb.append("\naddr : ");
- sb.append(poiLocation.getAddrStr());
- }
- if(poiLocation.hasPoi()){
- sb.append("\nPoi:");
- sb.append(poiLocation.getPoi());
- }else{
- sb.append("noPoi information");
- }
- logMsg(sb.toString());
- }
- }
設置參數
設置定位參數包括:定位模式(單次定位,定時定位),返回座標類型,是否打開GPS等等。eg:
- LocationClientOption option = new LocationClientOption();
- option.setOpenGps(true);
- option.setAddrType("detail");
- option.setCoorType("gcj02");
- option.setScanSpan(5000);
- option.disableCache(true);//禁止啓用緩存定位
- option.setPoiNumber(5); //最多返回POI個數
- option.setPoiDistance(1000); //poi查詢距離
- option.setPoiExtraInfo(true); //是否需要POI的電話和地址等詳細信息
- mLocClient.setLocOption(option);
發起定位請求
發起定位請求。請求過程是異步的,定位結果在上面的監聽函數onReceiveLocation中獲取。
- if (mLocClient != null && mLocClient.isStarted())
- mLocClient.requestLocation();
- else
- Log.d("LocSDK_2.0_Demo1", "locClient is null or not started");
發起POI查詢請求
發起POI查詢請求。請求過程是異步的,定位結果在上面的監聽函數onReceivePoi中獲取。
- if (mLocClient != null && mLocClient.isStarted())
- mLocClient.requestPoi();
位置提醒使用
位置提醒最多提醒3次,3次過後將不再提醒。 假如需要再次提醒,或者要修改提醒點座標,都可通過函數SetNotifyLocation()來實現。
- //位置提醒相關代碼
- mNotifyer = new NotifyLister();
- mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4個參數代表要位置提醒的點的座標,具體含義依次爲:緯度,經度,距離範圍,座標系類型(gcj02,gps,bd09,bd09ll)
- mLocationClient.registerNotify(mNotifyer);
- //註冊位置提醒監聽事件後,可以通過SetNotifyLocation 來修改位置提醒設置,修改後立刻生效。
- //BDNotifyListner實現
- public class NotifyLister extends BDNotifyListener{
- public void onNotify(BDLocation mlocation, float distance){
- mVibrator01.vibrate(1000);//振動提醒已到設定位置附近
- }
- }
- //取消位置提醒
- mLocationClient.removeNotifyEvent(mNotifyer);