導(dǎo)入庫文件
在下載頁面下載最新的庫文件衅鹿。將liblocSDK2.4.so文件拷貝到libs/armeabi目錄下玄括。將locSDK2.4.jar文件拷貝到工程根目錄下(libs根目錄下),并在工程屬性->Java Build Path->Libraries中選中“Add JARs”,選定locSDK2.4.jar.確定后返回庆捺。這樣您就可以在程序中使用百度定位API了。
設(shè)置AndroidManifest.xml
為區(qū)分2.3版本service,需要將manifest file中的intent filter聲明為com.baidu.location.service_v2.4在application標(biāo)簽中聲明service組件愁茁。
<Service android:name = "com.baidu.location.f" andrdoid: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>
聲明使用權(quán)限
<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相關(guān)類
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類型的參數(shù)鹅很。
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
public void onCreate() {
mLocationClient = new LocationClient(this); //聲明LocationClient類
mLocationClient.registerLocationListener(myListener);//注冊(cè)監(jiān)聽函數(shù)
}
實(shí)現(xiàn)BDLocationListener接口
BDLocationListener接口有2個(gè)方法需要實(shí)現(xiàn):
接收異步返回的的定位結(jié)果,參數(shù)是BDLocation類型參數(shù)罪帖。
-
接收異步返回的POI查詢結(jié)果促煮,參數(shù)是BDLocation類型參數(shù)。
public class MyLocationListener 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()); }
設(shè)置參數(shù)
設(shè)置定位參數(shù)包括:定位模式(單次定位整袁;定時(shí)定位)菠齿,返回坐標(biāo)類型,是否打開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個(gè)數(shù)
option.setPoiDistance(1000); //poi查詢距離
option.setPoiExtraInfo(true); //是否需要POI的電話和地址等詳細(xì)信息
mLocClient.setLocOption(option);
發(fā)起定位請(qǐng)求
發(fā)起定位請(qǐng)求绳匀。請(qǐng)求過程是異步的,定位結(jié)果在上面的監(jiān)聽函數(shù)onReceiveLocation中獲取炸客。
if (mLocClient != null && mLocClient.isStarted())
mLocClient.requestLocation();
else
Log.d("LocSDK_2.0_Demo1", "locClient is null or not started");
發(fā)起POI請(qǐng)求
發(fā)起POI查詢請(qǐng)求疾棵。請(qǐng)求過程是異步的,定位結(jié)果在上面的監(jiān)聽函數(shù)onReceiverPoi中后去痹仙。
if (mLocClient != null && mLocClient.isStarted())
mLocClient.requestPoi();
位置提醒使用
位置提醒最多提醒3次是尔,3次過后將不再提醒。 假如需要再次提醒开仰,或者要修改提醒點(diǎn)坐標(biāo)拟枚,都可通過函數(shù)SetNotifyLocation()來實(shí)現(xiàn)。
//位置提醒相關(guān)代碼
mNotifyer = new NotifyLister();
mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4個(gè)參數(shù)代表要位置提醒的點(diǎn)的坐標(biāo)众弓,具體含義依次為:緯度恩溅,經(jīng)度,距離范圍谓娃,坐標(biāo)系類型(gcj02,gps,bd09,bd09ll)
mLocationClient.registerNotify(mNotifyer);
//注冊(cè)位置提醒監(jiān)聽事件后脚乡,可以通過SetNotifyLocation 來修改位置提醒設(shè)置,修改后立刻生效傻粘。
//BDNotifyListner實(shí)現(xiàn)
public class NotifyLister extends BDNotifyListener{
public void onNotify(BDLocation mlocation, float distance){
mVibrator01.vibrate(1000);//振動(dòng)提醒已到設(shè)定位置附近
}
}
//取消位置提醒
mLocationClient.removeNotifyEvent(mNotifyer);
核心類
LocationClient類
用來發(fā)起定位每窖,添加取消監(jiān)聽
LocationClientOption類
用來設(shè)置定位方式帮掉,包括是否啟用緩存,使用GPS窒典,時(shí)間間隔等蟆炊。
BDLocation類
定位結(jié)果的封裝,包含坐標(biāo)信息和錯(cuò)誤代碼等信息瀑志。
BDLocationListener接口類
獲取定位結(jié)果
BDNotifyListener類
作用:位置提醒接口類涩搓,用于設(shè)置位置提醒點(diǎn),以及實(shí)現(xiàn)監(jiān)聽函數(shù)劈猪。這個(gè)應(yīng)該是適用于地圖昧甘。
使用方法:
定位是以service方式在運(yùn)行,所以需要在manifest.xml中聲明service組件战得。
聲明權(quán)限
MapView顯示地圖(百度API)
添加相關(guān)文檔
在application中添加開發(fā)秘鑰
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="開發(fā)者 key" />
</application>
添加所需權(quán)限
布局
<com.baidu.mapapi.map.MapView
android:id = "@id/bmapView"
android:layout_width="fill_parent"
android:layout_height = "fill_parent"
android:clickable="true"
/>
在應(yīng)用程序創(chuàng)建時(shí)初始化SDK引用的Context全局變量:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各組件之前初始化context信息充边,傳入
ApplicationContext //注意該方法要再
setContentView方法之前實(shí)現(xiàn)
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
}
}
管理地圖生命周期
public class MainActivity extends Activity
MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各組件之前初始化context信息,傳入ApplicationContext
//注意該方法要再setContentView方法之前實(shí)現(xiàn)
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
//獲取地圖控件引用
mMapView = (MapView)
findViewById(R.id.bmapView);
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity執(zhí)行onDestroy時(shí)執(zhí)行mMapView.onDestroy()常侦,實(shí)現(xiàn)地圖生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity執(zhí)行onResume時(shí)執(zhí)行mMapView. onResume ()浇冰,實(shí)現(xiàn)地圖生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity執(zhí)行onPause時(shí)執(zhí)行mMapView. onPause (),實(shí)現(xiàn)地圖生命周期管理
mMapView.onPause();
}
}
到這里就可以顯示地圖了聋亡,但是沒有定位肘习。
Android使用百度LBS SDK--定位實(shí)現(xiàn)
分別聲明一個(gè)MapView和BaiduMap:
private MapView mMapView;
private BaiduMap mBaiduMap;
在setContentView之前要初始化SDK
SDKInitializer.initialize(getApplication());
更換定位圖標(biāo)的方法
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo); // 自定義圖標(biāo)
// mCurrentMarker =null ; // 默認(rèn)圖標(biāo)
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
定位模式:
private LocationMode mCurrentMode;
mCurrentMode = LocationMode.NORMAL;//普通模式
//mCurrentMode = LocationMode.FOLLOWING;//跟隨模式
//mCurrentMode = LocationMode.COMPASS;//羅盤模式
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
初始化:
//地圖初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 開啟定位圖層
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打開gps
option.setCoorType("bd09ll"); // 設(shè)置坐標(biāo)類型
option.setScanSpan(5000);
mLocClient.setLocOption(option);
mLocClient.start();
setScanSpan是定位時(shí)間間隔(ms),
setCoorType坐標(biāo)類型分為三種:
bd09ll百度加密經(jīng)緯度坐標(biāo)
bd09百度加密墨卡托坐標(biāo)
gcj02國測局加密經(jīng)緯度坐標(biāo)
定位SDK監(jiān)聽函數(shù)
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location){
if(location ==null||mMapView = null)
return;
MylocationData = locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設(shè)置開發(fā)者獲取到的方向信息坡倔,順時(shí)針0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
參考
Android使用百度LBS SDK(一)顯示地圖MapView
http://blog.csdn.net/zhoumushui/article/details/41751259?utm_source=tuicool&utm_medium=referral