0

我正在开发一个 android 应用程序,其中我需要非常频繁地更新用户位置。每分钟说 2 次。

早些时候,我一直在使用 Google Play 服务的“融合位置服务”,但没有按要求收到位置更新。位置更新卡住了一段时间,更新之间的间隔跳到了 10 分钟左右。有时即使我将优先级设置为“PRIORITY_HIGH_ACCURACY”,也会发生同样的情况。

然后我回到旧的“位置管理器”,当我使用“NETWORK_PROVIDER”时,我注意到位置更新由于这个提供程序而卡住了。GPS也不会立即激活,需要一些时间。我正在尝试构建我的自定义融合位置提供程序。我怎样才能有效地在提供商之间切换,而不会延迟位置更新。

我想知道什么是定期获取位置更新的最佳实践,无论是 NW、GPS 还是两者兼而有之。就像它应该适用于无法提供位置更新卡住的应用程序。

电池消耗现在对我来说不是问题。我知道 Google 提供的有关位置访问的所有支持文档。

任何帮助将非常感激。

谢谢 !

4

1 回答 1

1

FusedLocationProvider really is the best option for obtaining locations at the moment, because it uses a lot more than just GPS or Network data to obtain location fixes. I have experienced issues regarding intervals being missed as well, but ultimately this is something down to luck depending on availability of GPS, Network, etc. etc.

My favourite usage of FusedLocationProvider so far is in conjunction with the AlarmManager class: Basically, the idea is to start location tracking at intervals specified by the Alarm Manager (this can be set to every 30 seconds such as in your case). Once the interval is hit, the location provider is to obtain a location fix as soon as possible (so in the setInterval method or whatever it's called, the parameter is a 0). This way, you can avoid having to wait another 30 seconds for a new location, instead having the location tracker attempt to provide a location as soon as possible after the interval specified by the Alarm Manager is hit.

By the way, when making custom location tracking wrappers, be careful of using the .getLastKnownLocation() method as it only uses cached locations - you could end up sending the same location to the user every 30 seconds.

Good luck!

于 2014-03-12T23:09:35.367 回答