当前位置: 移动技术网 > 移动技术>移动开发>Android > Android GPS室内定位问题的解决方法(location为null)

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

为什么室内没有location呢?

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

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

用百度位置定位sdk

sdk下载:
sdk使用:

1.  申请百度的服务密钥,具体操作步骤见官网:
2.将上面下载的sdk文件locsdk_4.1.jar拷贝到你项目的libs下
3.  修改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;
 }
}

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网