简体   繁体   English

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

[英]Location returned is null when provider is gps?

I am trying to retrieve the my current location coordinates in the following piece of code我正在尝试在以下代码中检索我当前的位置坐标

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);

I get the location coordinates when the provider is " network ".当提供者为“网络”时,我得到位置坐标。

When I change the settings in the location and security options to enable " Use GPS satellites " option the provider changes to " gps ".当我更改位置和安全选项中的设置以启用“使用 GPS 卫星”选项时,提供商更改为“ gps ”。 In this case the location returned is null.在这种情况下,返回的位置为空。

What could be the cause of this and how can it be rectified?这可能是什么原因,如何纠正?

It takes a while for the GPS provider to get a fix, especially if no previous fix was made. GPS 提供商需要一段时间才能进行修复,尤其是在之前未进行修复的情况下。 The getLastKnownLocation is not a blocking call that will wait untill an actual live GPS fix is established (that fix can take up to 1 min, depending on various reasons). getLastKnownLocation 不是阻塞调用,它会等到建立实际的实时 GPS 定位(该定位可能需要长达 1 分钟,取决于各种原因)。

Also, make sure you're outside for obvious reasons).另外,请确保您出于显而易见的原因在外面)。

Instead of simply calling the getLastKnownLocation for the GPS provider, you first need to request location updates from it.不是简单地为 GPS 提供者调用 getLastKnownLocation,您首先需要从它请求位置更新。

The following article explains the principle very well: http://developer.android.com/guide/topics/location/obtaining-user-location.html下面这篇文章很好地解释了原理: http : //developer.android.com/guide/topics/location/obtaining-user-location.html

It's a must read for anyone doing anything with the location provider on Android.对于在 Android 上使用位置提供程序进行任何操作的任何人来说,它都是必读的。

the typical flow is:典型的流程是:

  1. Start application.开始申请。
  2. Start listening for updates from desired location providers.开始侦听来自所需位置提供商的更新。
  3. Maintain a "current best estimate" of location by filtering out new, but less accurate fixes.通过过滤掉新的但不太准确的修复来保持位置的“当前最佳估计”。
  4. Stop listening for location updates.停止监听位置更新。
  5. Take advantage of the last best location estimate.利用最后的最佳位置估计。

Looking at the code below, we're initializing a MyLocationListener (to track the phone's position), and retrieving a reference to the LocationManager.查看下面的代码,我们正在初始化一个 MyLocationListener(以跟踪手机的位置),并检索对 LocationManager 的引用。 We're requesting location updates from the locationmanager.我们正在从位置管理器请求位置更新。 We're using a minDistance (minimum distance interval for notifications) of 10 meters, and a minTime (the minimum time interval for notifications) of 35 seconds.我们使用 10 米的 minDistance(通知的最小距离间隔)和 35 秒的 minTime(通知的最小时间间隔)。

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

When implementing this, you'll probably notice that the MyLocationListener receives more updates than you expected (given the minTime and minDistance parameters).在实现这一点时,您可能会注意到 MyLocationListener 收到的更新比您预期的要多(给定 minTime 和 minDistance 参数)。 The following article at http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ can help you understand why. http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ 上的以下文章可以帮助您理解原因。

I think the problem is that GPS take a fiew second to syncronice.我认为问题在于 GPS 需要几秒钟才能同步。 You must to use a thread.您必须使用线程。 Take this example:拿这个例子:

@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();
    }   
    
}

and add a Location Listener class并添加一个位置监听器类

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

It takes some seconds until the mobile connects to some satellites.移动设备连接到某些卫星需要几秒钟的时间。 As long as the phone has no connection, because the connection is not yet established, the location will be null.只要手机没有连接,因为连接还没有建立,位置就会为空。

You have to request location updates from the GPS provider too.您也必须向 GPS 提供商请求位置更新。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 除非已启用GPS,否则融合位置提供商无法获取位置 - Fused Location Provider not getting location unless GPS is on 当我读取GPS位置时,Android返回null - Android returns null when i read GPS location GPS定位仅在NETWORK_PROVIDER上有效? - GPS location working only on NETWORK_PROVIDER? GPS提供者需要很长时间才能获取位置信息时,使用endingIntent的Location Manager.RequestLocationUpdates始终会广播相同的位置 - Location Manager.RequestLocationUpdates using pendingIntent always broadcasting the same location, when gps provider takes long time to get location 无法使用GPS_PROVIDER / NETWORK_PROVIDER获取用户位置 - Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER 从 Fused Location Provider 获取 GPS 或网络提供商 - Obtain GPS or Network provider from Fused Location Provider 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent 网络提供商vs Gps提供者模拟位置Android - Network Provider vs Gps provider to mock location android 启用gps提供程序后关闭对话框 - Dismiss dialog when gps provider enabled Android:如何使用 GPS 提供商获取离线当前位置? - Android: How to get offline current location using GPS provider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM