导入库文件
在下载页面下载最新的库文件。将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 ;
-
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);
from: http://lszdb1983.blog.163.com/blog/static/20426348201272924223933/<wbr></wbr>