简体   繁体   English

如何每10秒更新一次GPS位置?

[英]How to update the GPS location for every 10 secs?

I have these variables and have the following call in the onResume method: 我有这些变量,并在onResume方法中进行以下调用:

  private Location userLocation;
  private GeoPoint point;
  private LocationManager manager;
  private LocationListener listener = new LocationListenerC();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 25, locationListener); 

but when I run this app in the android emulator, the location of the user is not updating every 10 secs. 但是当我在Android模拟器中运行此应用程序时,用户的位置不会每10秒更新一次。 Can anyone help? 有人可以帮忙吗?

The parameter mindTime doesn't mean you are going to receive updates every minTime milliseconds. 参数mindTime并不意味着您将 minTime毫秒接收更新。 It means you won't receive updates too often as less than minTime milliseconds, but doesn't specify when you will get them. 这意味着您不会经常收到小于minTime毫秒的更新,但是没有指定何时获取它们。 If the LocationManager has no new location to give you every 10 seconds, it simply won't give. 如果LocationManager没有新的位置每10秒给你一次,它根本就不会给出。 You will only receive an update when there's a new location. 只有在有新位置时,您才会收到更新。

You are using minDistance too. 你也在使用minDistance 25 means you won't receives new updates unless the device has moved at least 25 meters. 25表示除非设备移动了至少25米,否则您将不会收到新的更新。 The emulator doesn't move, so set this parameter to be 0 (Which means updates do not depend on whether the user has moved or not). 模拟器不移动,因此将此参数设置为0 (这意味着更新不取决于用户是否移动)。

The updates rate depends a lot on the user location. 更新率很大程度上取决于用户位置。 If, for example, the user is inside a building, it may take a while for the GPS/Network providers to receive updates from the satellites/base stations. 例如,如果用户在建筑物内,则GPS /网络提供商可能需要一段时间才能从卫星/基站接收更新。 So you may be getting updates in longer intervals. 所以你可能会在更长的时间间隔内获得更新。 However, if the user is outside and the device has good signal, updates may become extremely frequent (But you will get only 1 in every 10 seconds) 但是,如果用户在室外且设备信号良好,则更新可能会变得非常频繁(但每10秒钟只能获得1次)

If you want to get a location every 10 seconds, you can call getLastKnownLocation every 10 seconds. 如果您希望每10秒获取一个位置,则可以每10秒调用一次getLastKnownLocation Location may be the same though, if no updates from the providers have occured. 如果没有来自提供商的更新,则位置可能相同。

I think, The condition must be, time is 10(minimum) seconds AND distance is 25(minimum) meters. 我认为,条件必须是,时间是10(最小)秒,距离是25(最小)米。

ref: 参考:
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29 http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29

The minDistance parameter can also be used to control the frequency of location updates. minDistance参数还可用于控制位置更新的频率。 If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. 如果它大于0,那么位置提供程序将仅在位置至少改变minDistance米时向您的应用程序发送更新,并且至少已经过了minTime毫秒。 However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life. 但是,位置提供商使用minDistance参数更难以节省功耗,因此minTime应该是节省电池寿命的主要工具。

Do the following,as below 执行以下操作,如下所示

   private Location userLocation;
   private GeoPoint point;
   private LocationManager manager;
   private LocationListener listener = new LocationListenerC();

    //Change **locationManager** to **manager** 
    // and **locationListener** to **listener**  in the follwing line
   manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 25, listener); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM