android 百度地圖API使用教程說明
匯入庫檔案
在下載頁面下載最新的庫檔案。將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組件
- android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"
- android:permission="android.permission.BAIDU_LOCATION_SERVICE">
- android:name="com.baidu.location.service_v2.4">
-
-
聲明使用許可權
- android:name="android.permission.BAIDU_LOCATION_SERVICE">
- android:name="android.permission.BAIDU_LOCATION_SERVICE">
- android:name="android.permission.ACCESS_COARSE_LOCATION">
- android:name="android.permission.ACCESS_FINE_LOCATION">
- android:name="android.permission.ACCESS_WIFI_STATE">
- android:name="android.permission.ACCESS_NETWORK_STATE">
- android:name="android.permission.CHANGE_WIFI_STATE">
- android:name="android.permission.READ_PHONE_STATE">
- android:name="android.permission.WRITE_EXTERNAL_STORAGE">
- android:name="android.permission.INTERNET" />
- android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS">
- android:name="android.permission.READ_LOGS">
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 ;
- 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){
- 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 ;
- }
- StringBuffer sb = new StringBuffer(256);
- 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);