android使用百度定位SDK 定位主要有以下几步:
2.导入jar包及SO文件
3.配置androidManifest权限及服务
加入权限 :
在application标签下加入授权码:(每个授权码对应一个应用,需要自己申请)
android:name="com.baidu.lbsapi.API_KEY"
android:value="4ff8I8uXAuHPtpgyVw0GfUvY" />
部署定位服务
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
4.调用提供的接口
package com.util.location;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import android.content.Context;
import android.widget.Toast;
public class MyLocationManager {
private static MyLocationManager mInstance;
private Context mContext;
private LocationClient mLocationClient;
private BDLocationListener mListener = null;
private LocationEntry mCurrLocEntry;
private boolean hasResult = false;
public static final int LOCATION_OK = 161;
private int mRefreshTime = 5000;
private LocationClientOption mOption;
public static synchronized MyLocationManager getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new MyLocationManager(ctx);
}
return mInstance;
}
private MyLocationManager(Context ctx) {
mContext = ctx;
init();
}
private void init() {
// 初始化定位客户端
mLocationClient = new LocationClient(mContext);
// 初始化定位参数
mOption = new LocationClientOption();
initLocationOption();
mListener = new BDLocationListener() {
@Override
public void onReceivePoi(BDLocation location) {
}
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null) {
hasResult = false;
if (mResultListener != null) {
mResultListener.onLocationResult(hasResult, null);
}
return;
}
LocationEntry entry = new LocationEntry();
entry.locType = location.getLocType();
loadLocMsg(entry);
entry.city = location.getCity();
entry.longitude = location.getLongitude();
entry.latitude = location.getLatitude();
mCurrLocEntry = entry;
hasResult = true;
if (mResultListener != null) {
mResultListener.onLocationResult(hasResult, mCurrLocEntry);
}
}
};
// 注册定位广播
mLocationClient.registerLocationListener(mListener);
}
private void loadLocMsg(LocationEntry entry) {
String locMsg = "";
switch (entry.locType) {
case 61:
locMsg = "GPS定位结果";
break;
case 62:
locMsg = "扫描整合定位依据失败。此时定位结果无效";
break;
case 63:
locMsg = "网络异常,没有成功向服务器发起请求。此时定位结果无效";
break;
case 65:
locMsg = "定位缓存的结果";
break;
case 66:
locMsg = "离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果";
break;
case 67:
locMsg = "离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果";
break;
case 68:
locMsg = "网络连接失败时,查找本地离线定位时对应的返回结果";
break;
case LOCATION_OK:
locMsg = "定位正常";
break;
case 162:
case 163:
case 164:
case 165:
case 166:
case 167:
locMsg = "服务端定位失败";
break;
case 502:
locMsg = "key参数错误";
break;
case 505:
locMsg = "key不存在或者非法";
break;
case 601:
locMsg = "key服务被开发者自己禁用";
break;
case 602:
locMsg = "key mcode不匹配";
break;
default:
if (entry.locType > 501 && entry.locType <700) {
locMsg &#61; "key验证失败";
}
break;
}
entry.locMsg &#61; locMsg;
}
public boolean isStart() {
return mLocationClient.isStarted();
}
public boolean hasResult() {
return hasResult;
}
/**
* 初始化定位参数
*/
public void initLocationOption() {
mOption.setOpenGps(false);
mOption.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
mOption.setCoorType("bd09ll");// 返回的定位结果是百度经纬度&#xff0c;默认值gcj02
mOption.setScanSpan(mRefreshTime);// 设置发起定位请求的间隔时间为5000ms
mOption.setIsNeedAddress(true);// 返回的定位结果包含地址信息
mOption.setAddrType("all");
mOption.setNeedDeviceDirect(false);// 返回的定位结果包含手机机头的方向
}
/**
* 设置产品名&#xff0c;功能未知
*
* &#64;param name
*/
public void setProdName(String name) {
mOption.setProdName(name);
}
/**
* 设置刷新时间
*
* &#64;param time
*/
public void setRefreshTime(int time) {
mRefreshTime &#61; time;
}
public int getRefreshTime() {
return mRefreshTime;
}
/**
* 开始定位
*/
public void start() {
if (mLocationClient !&#61; null) {
mLocationClient.setLocOption(mOption);
mLocationClient.start();
} else {
Toast.makeText(mContext, "locClient is null ", Toast.LENGTH_SHORT)
.show();
}
}
/**
* 停止定位
*/
public void stop() {
if (mLocationClient !&#61; null && mLocationClient.isStarted()) {
mLocationClient.stop();
}
}
public void destory() {
stop();
mLocationClient &#61; null;
mListener &#61; null;
mCurrLocEntry &#61; null;
mInstance &#61; null;
}
public LocationEntry getLocationEntry() {
return mCurrLocEntry;
}
private OnLocationResultListener mResultListener;
public void setOnLocationResultListener(OnLocationResultListener l) {
mResultListener &#61; l;
}
public class LocationEntry {
int locType;
String locMsg;
String city;
double longitude;
double latitude;
}
public interface OnLocationResultListener {
void onLocationResult(boolean hasResult, LocationEntry entry);
}
}