为什么室内没有location呢?
因为我们开发的时候几乎肯定都是在室内的,这个时候卫星你是搜索不到的,所以必然是定位不了的,所以系统如何将位置信息通知给你的程序。所以要从根本上解决这个问题,就要解决位置信息获取问题。
那么我来告诉大家,只有NETWORK_PROVIDER这种模式才是室内定位可靠的方式,就是当location为null的时候只要用这个,NETWORK_PROVIDER。
不过直接用大家也是用不了的,为啥呢,因为大部分厂商也不会用google的服务,这种定位方式默认是没法用的。那怎么办?好办,找个替代的服务商就可以了,百度或者高德的位置信息sdk就可以解决这个问题。它的基本原理在上面已经提到过了,就是搜集你的wifi节点信息和你的手机基站信息来定位。
本篇文章我们来用百度解决。
用百度位置定位SDK
SDK下载:
http://lbsyun.baidu.com/sdk/download
SDK使用:
- 申请百度的服务密钥,具体操作步骤见官网: http://api.map.baidu.com/lbsapi/cloud/geosdk.htm
- 将上面下载的sdk文件locSDK_4.1.jar拷贝到你项目的libs下
- 修改AndroidManifest文件,在该文件里添加如下配置
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="xxxxx " />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
上面meta-data中value的值改为你自己的密钥
代码里调用sdk:
public class LocationUtil {
private final static boolean DEBUG = true;
private final static String TAG = "LocationUtil";
private static LocationUtil mInstance;
private BDLocation mLocation = null;
private MLocation mBaseLocation = new MLocation();
public static LocationUtil getInstance(Context context) {
if (mInstance == null) {
mInstance = new LocationUtil(context);
}
return mInstance;
}
Context mContext;
String mProvider;
public BDLocationListener myListener = new MyLocationListener();
private LocationClient mLocationClient;
public LocationUtil(Context context) {
mLocationClient = new LocationClient(context.getApplicationContext());
initParams();
mLocationClient.registerLocationListener(myListener);
}
public void startMonitor() {
if (DEBUG) Log.d(TAG, "start monitor location");
if (!mLocationClient.isStarted()) {
mLocationClient.start();
}
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.requestLocation();
} else {
Log.d("LocSDK3", "locClient is null or not started");
}
}
public void stopMonitor() {
if (DEBUG) Log.d(TAG, "stop monitor location");
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.stop();
}
}
public BDLocation getLocation() {
if (DEBUG) Log.d(TAG, "get location");
return mLocation;
}
public MLocation getBaseLocation() {
if (DEBUG) Log.d(TAG, "get location");
return mBaseLocation;
}
private void initParams() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
//option.setPriority(LocationClientOption.NetWorkFirst);
option.setAddrType("all");//返回的定位结果包含地址信息
option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
option.disableCache(true);//禁止启用缓存定位
option.setPoiNumber(5); //最多返回POI个数
option.setPoiDistance(1000); //poi查询距离
option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null) {
return ;
}
mLocation = location;
mBaseLocation.latitude = mLocation.getLatitude();
mBaseLocation.longitude = mLocation.getLongitude();
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());
sb.append("\ncity : ");
sb.append(location.getCity());
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());
}
if (DEBUG) Log.d(TAG, "" + sb);
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
public class MLocation {
public double latitude;
public double longitude;
}
}