【问题标题】:onLocationChanged always returns I old locationonLocationChanged 总是返回旧位置
【发布时间】:2013-03-28 06:25:56
【问题描述】:

我已经为我的 LocationManager 注册了位置更新,每 10 秒一次

mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this);

onLocationChanged 回调每 10 秒返回一个位置,该位置(位置)超过 2 小时。而且那个时间戳永远不会改变。

问题是:

2 小时前,我在一个完全不同的位置()在 wifi 上使用该设备。现在我在另一个 wifi 上的其他位置(office),我的应用程序将我当前的位置显示为 home。昨天在 home 也发生了同样的事情,当时它显示 office 是我当前的位置。当我关闭我的应用、打开 FourSquare 应用并重新打开我的应用时,它开始工作(开始显示正确的位置)。

完整代码:

public class LocationService extends Service implements LocationListener {

    public static double curLat = 0.0;
    public static double curLng = 0.0;
    private LocationManager mgr;
    private String best;
    private Location location;

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        best = LocationManager.NETWORK_PROVIDER;
        location = mgr.getLastKnownLocation(best);
        if (location != null) {

            dumpLocation(location);
            mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    10 * 1000, 50, this);
        }
        return START_NOT_STICKY;
        }
    }

    private void dumpLocation(Location l) {

        SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss",
                Locale.ENGLISH);
        String format = s.format(l.getTime());
        //The above time is always 28/03/2013:09:26:41 which is more than 2 hrs old
        curLat = l.getLatitude();
        curLng = l.getLongitude();
    }

    @Override
    public void onLocationChanged(Location location) {

        dumpLocation(location);
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

以这种方式在 Activity 中启动:

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
pi = PendingIntent.getService(this, 0, i,
        PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(), 10000, pi);

清单中的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

我现在可以通过打开其他一些基于位置的应用程序(例如 Maps、Navigator、Foursquare 等)获得正确的位置,但是为什么我的应用程序无法从提供商那里获得新的/全新的修复。

谢谢

【问题讨论】:

    标签: android locationmanager


    【解决方案1】:

    因为这条线,你的位置变老了

      location = mgr.getLastKnownLocation(best);
    

    因为如果 GPS 未启用,它会显示旧位置。所以删除此代码它将像冠军一样工作 也可以参考这个库

    https://github.com/nagendraksrivastava/Android-Location-Tracking-Library

    根据您的 cmets 我已经编辑了答案 好的 让我逐行解释 location = mgr.getLastKnownLocation(best); 它会给你最后知道位置的对象,然后它会进入 if 条件,它会调用 dumplocation 并获取最后的位置数据,然后你调用

    mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    10 * 1000, 50, this);
    

    但假设 GPS 提供程序被禁用,那么它不会获取新位置,因此您只会获得旧位置。所以要么你可以改变它像

    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                       {
                        locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,new NagendraSingleLocationListener(),null);
                       }
                       else
                       {
                           locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,new NagendraSingleLocationListener(),null);
                       }
    

    【讨论】:

    • 但是即使我使用mgr.getLastKnownLocation(best); requestLocationUpdates 是为了提供新的位置对吗?建议从mgr.getLastKnownLocation(best); 开始,然后注册更新。 AFAIK
    • 您可以查看我编辑的答案,如果您的问题已经解决,请告诉我
    • 是的。所以,问题不在于mgr.getLastKnownLocation(best)。我正在使用错误/禁用的提供程序来请求更新。 +1 非常感谢。
    • 是的,正是..我的荣幸阿奇
    【解决方案2】:

    我认为因为您立即取消了待处理的意图,因此 requestLocationUpdate 在您取消之前不会开始更新。你为什么不睡觉可能会在取消之前等待 2 秒。

    【讨论】:

    • 其实cancel就是移除之前分配的任何类似的待处理intent。运行服务也没有问题。甚至onLocationChanged 每 10 秒调用一次。唯一的问题是location it returns
    【解决方案3】:

    根据我的经验,即使 gps 没有足够的卫星工作,当你请求更新时,android 也会给你一个位置。因此,即使 gps 开启 - 如果您在内部或位置不好(例如在桥下),android 也会为您提供旧的修复。确实可以是很老的了。

    我发现唯一能 100% 工作的是一般不使用第一个位置,而只记住时间。当新职位到达时,您可以检查时间是否比上次更新。如果您只想使用非常精确的位置,您可能还需要检查 location.getAccuracy() 是否低(越低越好)。

    我使用 gps 来获取我的肥皂接口时间戳,因为 android 时钟有时会非常关闭,这是我从 gps 获取有效时间的唯一方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2022-01-22
      • 2013-01-07
      相关资源
      最近更新 更多