核心位置 - 后备、位置缓存和替代方案
我有几个关于核心位置的问题。
1) 如果用户拒绝我的应用程序使用核心位置的权限,或者核心位置由于某种原因不可用,是否有后备方案? (例如设备区域设置?)
2)我可以缓存设备的位置以供下次使用吗? Core Location 自己做这个吗?
3)我确实需要仲春时节用户所在区域的日落时间,一旦我获得了设备的纬度和经度,我就有一个函数可以做到这一点。也许我可以根据区域设置对时间做出假设? (示例:在美国,假设大约晚上 7:00。)
编辑:
我确实在尝试计算应用程序用户区域的日落。和地图没关系。我正在考虑以下事件顺序:
- 检查核心位置可用性。如果是,请使用它并将其存储在 NSUserPreferences 中。如果核心位置不可用,请继续后备。
- 检查存储的位置。如果已存储,请使用它。如果没有,请继续...
- 检查用户选择的时间。
I have a few questions about Core Location.
1) Should the user refuse permission for my app to use core location, or core location is unavailable for some reason, is there a fallback? (Device Locale, for example?)
2)Can I cache a device's location for next time? Does Core Location do this itself?
3)I really need the sunset time in the user's area during the mid-spring season and I have a function to do that, once I have the Latitude and Longitude of the device. Perhaps I can just make an assumption about the time based on Locale? (Example: In the US, assume approximately 7:00pm.)
EDIT:
I really am trying to calculate sunset in the users area for an app. Nothing to do with the map. I am considering the following sequence of events:
- Check for Core Location availability. If yes, use it and store it in NSUserPreferences. If Core Location is unavailable, go on to the fallbacks.
- Check for a stored Location. If it's stored, use it. If not, go on...
- Check for the user's chosen time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 严格来说,如果用户不允许使用 CoreLocation,或者已授予权限但 CoreLocation 不可用,则没有其他后备方法可以以纬度、经度对的形式获取用户位置。使用区域设置可能并不适用于所有情况。首先,它只会给你一个可能与现实相差太远的近似值。但是,由您来判断此近似值是否适合您的应用程序。此外,有些用户使用的语言环境与其所在国家/地区的语言环境不同。而且,您不能保证出国旅行的人每次都会调整日期/时间。
2) Core Location 默认缓存从 GPS 设备检索到的最后一个位置。然而,对于出国旅行的人来说,这个缓存的位置肯定是错误的(即使对于几英里外旅行的人来说也是如此),一般来说,您应该丢弃缓存的值并每次重新本地化用户。我知道对于在本国旅行的人来说,这(通常)不会造成巨大的问题。但这仅取决于国家/地区:在意大利境内旅行不会改变时间,但在美国旅行可能会改变时间最多 3 小时。
3)我建议尝试使用核心位置,如果出现问题,只需要求用户输入他/她的当地时间或位置(城市应该足以满足您的目的)。如果您选择询问用户的位置,那么您可以获得相应的纬度/经度对,但这需要有效的网络连接。
1) Strictly speaking, if the user does not allow using CoreLocation or, if permission were given but CoreLocation is not available, there is no other fallback to get the user's location as a latitude, longitude pair. Using the locale may not work in all of the cases. First, it will just give you an approximation which may be way too far from reality. However, it's up to you to judge whether this approximation is ok for your app. Moreover, there are some users using a locale different from the one related to their country. And, you have no guarantee that people travelling abroad will adjust the date/time every time.
2) Core Location caches by default the last location retrieved from the GPS unit. However, for people travelling abroad this cached location will be certainly wrong (even for people travelling just a few miles away), and in general, you should discard the cached value and localize again the user each time. I understand that for people travelling within their country this will not (usually) create huge problems. But it just depends on the country: travelling within Italy will not change the time, but travelling across the US may change the time up to 3 hours.
3) I would suggest to try using Core Location and, in case of problems, simply ask the user to input his/her local time or location (the city should be enough for your purpose). If you choose to ask for the user's location, then you can get the corresponding latitude/longitude pair but this will require a working network connection.
2)来自 CLLocationManager 的在线文档:
您可以检查 CLLocation 时间戳以查看它是否是缓存值。
3) 如果您确实决定使用区域设置进行初始 TZ 近似,请记住用户会旅行,并且可能不会立即重置其区域设置。您可以通过以下方式获取当前 TZ 对象:
2) From the online docs for CLLocationManager:
You can check the CLLocation timestamp to see if it is a cached value or not.
3) If you do decide to use locale for an initial TZ approximation, remember that users travel, and may not reset their locale promptly. You can get the current TZ object with: