【问题标题】:Location returned is null when provider is gps?提供者为 gps 时返回的位置为空?
【发布时间】:2011-04-15 08:53:39
【问题描述】:

我正在尝试在以下代码中检索我当前的位置坐标

LocationManager locationManager= (LocationManager) getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);

当提供者为“network”时,我得到了位置坐标。

当我更改位置和安全选项中的设置以启用“使用 GPS 卫星”选项时,提供商更改为“gps”。 在这种情况下,返回的位置为空。

可能是什么原因造成的,如何纠正?

【问题讨论】:

    标签: java android


    【解决方案1】:

    GPS 提供商需要一段时间才能得到修复,尤其是在之前没有修复的情况下。 getLastKnownLocation 不是阻塞调用,它会等到建立实际的实时 GPS 定位(该定位可能需要长达 1 分钟,具体取决于各种原因)。

    另外,请确保您出于明显的原因在外面)。

    您首先需要从它请求位置更新,而不是简单地为 GPS 提供者调用 getLastKnownLocation。

    下面这篇文章很好的解释了原理:http://developer.android.com/guide/topics/location/obtaining-user-location.html

    对于在 Android 上使用位置提供程序进行任何操作的人来说,这是一本必读的书。

    典型的流程是:

    1. 启动应用程序。
    2. 开始监听来自所需位置提供商的更新。
    3. 通过过滤掉新的但不太准确的修复来维护“当前最佳估计”位置。
    4. 停止监听位置更新。
    5. 利用最后的最佳位置估计。

    查看下面的代码,我们正在初始化一个 MyLocationListener(用于跟踪手机的位置),并检索对 LocationManager 的引用。我们正在向位置管理器请求位置更新。我们使用 10 米的 minDistance(通知的最小距离间隔)和 35 秒的 minTime(通知的最小时间间隔)。

    LocationListener locationListener = new MyLocationListener();
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
    

    在实现这一点时,您可能会注意到 MyLocationListener 收到的更新比您预期的要多(给定 minTime 和 minDistance 参数)。以下http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ 的文章可以帮助您了解原因。

    【讨论】:

    • 是的,我试过这样,它工作正常,当我关闭我的设备并再次打开并运行应用程序时,它又给了我同样的问题
    【解决方案2】:

    我认为问题在于 GPS 需要几秒钟才能同步。您必须使用线程。举个例子:

    @Override
    public void run() {
        
        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Looper.prepare();
            mToast.Make(getContext(),"GPS",0);
            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
            Looper.loop(); 
            Looper.myLooper().quit(); 
            
        } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
            Looper.prepare();
            mToast.Make(getContext(),"Triangulacion",0);
            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
            Looper.loop();
            Looper.myLooper().quit();
        }else{
            mToast.Make(context,"No se encuentra señal se procede a mandar un mensaje normal",0);
            Looper.prepare();
            handlerNormal.sendEmptyMessage(0);
            Looper.loop();
            Looper.myLooper().quit();
        }   
        
    }
    

    并添加一个 Location Listener 类

    private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                setCurrentLocation(loc);
                handler.sendEmptyMessage(0);
            }
        }  
    //...  
    } 
    

    【讨论】:

      【解决方案3】:

      手机连接到某些卫星需要几秒钟的时间。只要手机没有连接,因为连接还没有建立,位置就会为空。

      您还必须向 GPS 提供商请求位置更新。

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-13
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多