栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android GPS室内定位问题的解决方法(location为null)

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android GPS室内定位问题的解决方法(location为null)

为什么室内没有location呢?

       因为我们开发的时候几乎肯定都是在室内的,这个时候卫星你是搜索不到的,所以必然是定位不了的,所以系统如何将位置信息通知给你的程序。所以要从根本上解决这个问题,就要解决位置信息获取问题。
        那么我来告诉大家,只有NETWORK_PROVIDER这种模式才是室内定位可靠的方式,就是当location为null的时候只要用这个,NETWORK_PROVIDER。
        不过直接用大家也是用不了的,为啥呢,因为大部分厂商也不会用google的服务,这种定位方式默认是没法用的。那怎么办?好办,找个替代的服务商就可以了,百度或者高德的位置信息sdk就可以解决这个问题。它的基本原理在上面已经提到过了,就是搜集你的wifi节点信息和你的手机基站信息来定位。 

        本篇文章我们来用百度解决。

用百度位置定位SDK

SDK下载:http://lbsyun.baidu.com/sdk/download
SDK使用:

1.  申请百度的服务密钥,具体操作步骤见官网:http://api.map.baidu.com/lbsapi/cloud/geosdk.htm
2.将上面下载的sdk文件locSDK_4.1.jar拷贝到你项目的libs下
3.  修改AndroidManifest文件,在该文件里添加如下配置       




 
 
 

上面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;
 }
}

当然别忘了在setting里将gps定位打开。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/159742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号