位置变化始终返回旧位置

9

我已经注册了我的位置管理器(LocationManager)以便每10秒更新位置信息。

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

但是onLocationChanged回调每10秒返回一次位置,这个位置已经超过2小时了。时间戳从未改变。
问题在于:2小时前我在完全不同的地方(家里)使用设备上的wifi。现在我在另一个地方(办公室)使用不同的wifi,我的应用程序显示我的当前位置为家。昨天在家时也出现了同样的情况,当时它将我的当前位置显示为办公室。当我关闭我的应用程序,打开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" />

我现在可以通过打开其他基于位置的应用程序(如地图、导航器、Foursquare等)来获取正确的位置。但是为什么我的应用程序无法从提供者那里获得新的/新鲜的定位呢?谢谢。
3个回答

4

你之所以得到旧的位置,是因为这行代码

  location = mgr.getLastKnownLocation(best);

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

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

根据您的评论,我已编辑了答案。好的,让我逐行解释。 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); 开始,然后再注册更新。据我所知。 - Archie.bpgc
你可以查看我的编辑答案,请告诉我你的问题是否已解决。 - Naga
是的。所以,问题不在于 mgr.getLastKnownLocation(best)。我正在使用错误/禁用的提供程序来请求更新。再次感谢。 - Archie.bpgc
没错,阿奇,我很乐意。 - Naga

0

我认为是因为您立即取消了待处理的意图,因此在您取消之前,requestLocationUpdate不会开始更新。为什么不在取消之前睡眠2秒钟呢?


实际上,cancel 是为了删除之前分配的任何类似的挂起意图。运行服务没有问题。甚至每 10 秒钟都会调用 onLocationChanged。唯一的问题是它返回的 位置信息 - Archie.bpgc

0

根据我的经验,即使 GPS 卫星数量不足以工作,当您请求更新时,Android 也会提供位置。因此,即使 GPS 打开 - 如果您在室内或处于不良位置(如桥下),Android 也会向您提供旧的修复程序。可能确实是非常古老的。

我发现唯一百分之百有效的方法是通常不使用第一个位置,只记住时间。当新位置到达时,您可以检查时间是否比上次更新更近。如果您只想使用非常精确的位置,则还需要检查 location.getAccuracy() 是否低(越低越好)。

我使用 GPS 获取我的时间戳以用于 SOAP 接口,因为 Android 时钟有时可能会出错,这是我从 GPS 获取有效时间的唯一方法。


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