简体   繁体   English

按需获取设备位置

[英]Get device location onDemand

So there is a lot of talk about getting device location. 因此,关于获取设备位置的讨论很多。 My need is very particular and I am wondering if there is an exact answer. 我的需求非常特殊,我想知道是否有确切答案。

  • I do not want to track users. 我不想跟踪用户。
  • There is a specific activity in my app that requires location. 我的应用程序中有一项特定的活动需要定位。 So when a user go take that specific action, I need to get the location of the device. 因此,当用户执行该特定操作时,我需要获取设备的位置。
  • I need the location acquisition to happen very fast 我需要快速进行位置获取
  • I want FINE location. 我想要好位置。

The Android example seems to be about coarse location only https://developer.android.com/training/location/retrieve-current.html Android示例似乎仅关于粗略的位置https://developer.android.com/training/location/retrieve-current.html

And the answers here seem to be about tracking a user. 此处的答案似乎与跟踪用户有关。

For my app, the action in question is not frequent at all (maybe once or twice a week), but when the user wants to use that feature, I need the finest current location. 对于我的应用程序,所涉及的操作根本就不频繁(也许每周一次或两次),但是当用户想要使用该功能时,我需要最好的当前位置。

So is my only solution to track the user? 那么,我唯一的跟踪用户的解决方案是吗?

The documentation you linked specifies: 您链接的文档指定:

The permission you choose determines the accuracy of the location returned by the API 您选择的权限决定了API返回的位置的准确性

and later 然后

The precision of the location returned by this call is determined by the permission setting you put in your app manifest, as described in the Specify App Permissions section of this document. 如本文档的“指定应用程序权限”部分所述,此调用返回的位置的精度取决于您放置在应用程序清单中的权限设置。

So if you add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> then you will get fine location. 因此,如果您添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>那么您将获得很好的位置。

That said, there is possibility you will not get location, because there is no last location for the user (it was retrieved long time ago, for example). 就是说,有可能您将无法获得位置,因为用户没有最后的位置 (例如,很久以前就已检索到)。

The getLastLocation() method returns a Location object (...). getLastLocation()方法返回一个Location对象(...)。 The location object returned may be null in rare cases when the location is not available. 在极少数情况下,当位置不可用时,返回的位置对象可能为null。

Then you would just need to start tracking user location, as per documentation , but after you get first update you can immediately stop tracking and release resources. 然后,您只需按照文档开始跟踪用户位置,但是在第一次更新后,您可以立即停止跟踪并释放资源。 This will give you location almost for sure (if it's available), but maybe not right away. 这几乎可以肯定地为您提供位置(如果有的话),但可能不会立即提供。

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

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