Android定位详解 兼容网络定位、GPS定位

Android6.0 高通上面跑

D:\AndroidStudioProjects\MyGPSTest

https://www.iteye.com/blog/gundumw100-2160728

基于android的定位无非就两种:network、gps。两者各有优劣。
GPS定位的 好处 :精确度高; 坏处 :仅能在户外使用,获取定位信息速度慢,耗费电池。 
网络定位的 好处 :户内户外都能使用,定位速度快,电量耗费低; 坏处 :精确度不太高。

定位主要是这两个方法 getBestProvider()、requestLocationUpdates();

 

locationManager.getBestProvider(criteria,true);方法看起来很完美,但其实返回值就network、gps二选一。而且如果你设置里面是高精度(网络与GPS),它会优先检查GPS provider 返回的是GPS;如果设置里面选择了节电(网络)provider 就返回network;如果手机开启了仅限设备(GPS)就返回GPS;如果都没开启则返回null。

其实通过bestProvider也是可以实现GPS跟网络一起定位的,只是第一次需要通过GPS定位成功了,后面没有GPS了(到室内)才会切换到网络定位。

 

注意requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,networkListener)如果设置第一个参数为网络定位,第三个参数设置为0。第三个参数为位置变化最小距离:当位置距离变化超过此值时,将更新位置信息。如果位置不变定位是不更新的。

//注册监听    
 locationManager.requestLocationUpdates(provider, 60000, 0,new MyLocationListener());

举个例子:如果你在室内,gps无法定位到,你的程序将陷入死循环。当然使用requestLocationUpdates可以做到定位且不让程序陷入死循环,但是定位耗时长,甚至得不到定位。
如果使用网络定位呢,不得说这也是一个不错的选择。locationManager.requestLocationUpdates(
              LocationManager.NETWORK_PROVIDER, 0, 0,networkListener);
网络定位耗时一般在2秒左右(网络差,时间会更长),只要你接入网络,基本上都能获得定位。唯一的缺点就是精度不高。

那能不能将两者结合,这也是本文的重点。既然结合两者,就要同时为两者添加监听

Java代码

 

  1. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000 * 2,50,gpsListener);  
  2. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,networkListener);  



这样,大概2秒我们就可以得到来自网络的定位,一分钟后得到来自GPS定位。这时用GPS定位替换网络定位就好了。当然这只是个理想的情况,现实要复杂的多。

GpsDemo.java中的代码:

    package com.example.demo;
     
    import android.app.Activity;
    import android.content.Context;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
     
    public class GpsDemo extends Activity {
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_gpsdemo);
        }
        
        //实现GPS的方法
        public void gps(){
            //定义LocationManager对象
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            
            //定义Criteria对象
            Criteria criteria = new Criteria();
            // 定位的精准度
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            // 海拔信息是否关注
            criteria.setAltitudeRequired(false);
            // 对周围的事情是否进行关心
            criteria.setBearingRequired(false);
            // 是否支持收费的查询
            criteria.setCostAllowed(true);
            // 是否耗电
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            // 对速度是否关注
            criteria.setSpeedRequired(false);
            
            //得到最好的定位方式    
            String provider = locationManager.getBestProvider(criteria, true);
            
            //注册监听    
            locationManager.requestLocationUpdates(provider, 60000, 0,new MyLocationListener());

            //在这里分别写GPS、网络的监听,就可以监听两个方式的定位了
        }
        
        //实现监听接口
        private final class MyLocationListener implements LocationListener {
            @Override// 位置的改变
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                double latitude = location.getLatitude();// 维度
                double longitude = location.getLongitude();// 经度
                //显示当前坐标
                Toast.makeText(GpsDemo.this, "location:("+latitude+","+longitude+")", Toast.LENGTH_LONG).show();
            }
     
            @Override// gps卫星有一个没有找到
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
            }
     
            @Override// 某个设置被打开
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
            }
     
            @Override// 某个设置被关闭
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
            }
        
         }
        
        //按钮点击事件的方法
        public void getLocation(View view) {
            this.gps();
        }
    }
Android定位详解 兼容网络定位、GPS定位_第1张图片

你可能感兴趣的:(Android)