Android 中的位置提供程序?
我的应用程序需要用户的当前位置。为此,我已经实现了代码,并且能够获取当前位置。该代码首先尝试获取最佳提供商,然后获取位置信息。问题是,如果禁用 GPS_PROVIDER,则当前位置将从 NETWORK_PROVIDER 返回。如果同时启用 NETWORK_PROVIDER 和 GPS_PROVIDER,则最好的提供商将是 GPS_PROVIDER,这会花费太多时间来获取当前位置。
那么,如果我们只将位置提供程序设置为NETWORK_PROVIDER,这样GPS_PROVIDER是否启用就无关紧要了。
请让我知道什么是正确的方法。
问候
苏尼尔
My application requires current location of the user. For this I have implemented the code and I am able to get the current location. The code tries to get the best provider first and then fetches location information. The problem is that the current location is returned from NETWORK_PROVIDER if GPS_PROVIDER is disabled. If both NETWORK_PROVIDER and GPS_PROVIDER are enabled then the best provider will be GPS_PROVIDER and this takes too much time to get the current location.
So, is it alright if we set the location provider as NETWORK_PROVIDER only so that it will not matter whether GPS_PROVIDER is enabled or not.
Please let me know what is the right approach.
Regards
Sunil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GPS 提供比网络提供商更好的精度。
网络提供商是使用蜂窝塔和 WiFi 的三角测量来完成的。
因此,如果您立即需要,请从“最后已知位置”开始,然后切换到网络,一旦您从 GPS 获得修复,就开始使用它。
问候,
维奈
GPS gives better precision than Network provider.
Network provider is done using triangulation of cell towers and wifi.
So, if you need immediately, start with "last known location" then switch to network, once you have the fix from the gps, start using it.
regards,
vinay
根据您的应用程序,您可能只能使用网络位置。对于许多基于位置的服务来说,这已经足够细粒度了。另外,您不必担心 GPS 无线电会耗尽用户的电池。
但如果您确实想要尽可能准确的位置,为什么不听取两家提供商的意见呢?您可以使用
getLastKnownLocation()
方法获取初始位置。然后您可以使用NETWORK_PROVIDER
位置。然后,一旦从GPS_PROVIDER
获得修复,您就可以忽略或删除网络侦听器。Depending on your application, you might be able to use just the network location. This is fine-grained enough for many location-based services. Plus you don't have to worry about sucking up the user's battery with GPS radio.
But if you really want as accurate a location as possible, why not listen to both providers? You could use the
getLastKnownLocation()
method for the initial location. Then you can use theNETWORK_PROVIDER
location. Then, as soon as you get a fix from theGPS_PROVIDER
, you can ignore or remove the network listener.