当提供者为 GPS 时,返回的位置为空?

4

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

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”。 在这种情况下,返回的位置为空。

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

3个回答

7
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的引用。我们正在从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/可以帮助您理解原因。

是的,我尝试过这样做,它运行得很好。但当我关闭设备并再次打开并运行应用程序时,它又出现了同样的问题。 - user1891910

3

我认为问题在于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();
    }   
    
}

并添加一个位置监听器类

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

2

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

您还需要请求GPS提供程序的位置更新。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接