iOS位置发现检测
目前我正在构建一个应用程序,它根据当前位置显示附近的广告。当用户第一次启动应用程序时,我希望他们看到附近排名前 10 的广告。
我在按时获取位置时遇到了一个小问题。我的问题是在加载视图之前尚未获取当前位置。 当然有委托 UpdatedLocation,但是这个委托被多次触发。我猜它会触发多次,因为它想获取最准确的位置?
所以我的问题是;我该怎么做才能“等待”找到位置,然后开始搜索广告?
提前致谢!
问候, 米切尔
Currently I'm building an application which shows nearby advertisements based on the current location. When the user starts the application for the first time I want them to see a top 10 of nearby advertisements.
I'm having a slight problem with fetching the location on time. My problem is that the current location isn't fetched yet before the view is loaded.
Of course there is the delegate UpdatedLocation, but this one gets fired multiple times. I guess it fires multiple times, because it wants to fetch the most accurate location?
So my question is; What can I do to 'wait' until the location is found and then start searching for advertisements?
Thanks in advance!
Regards,
Mittchel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次获得(更准确的)位置时,您都应该更新视图。因此,请记住最后一个位置,并检查
horizontalAccuracy
/verticalAccuracy
是否已减小(较小 = 更准确,但负值 = 无效)。也许每次获得新位置时进行更新都会更好,因为准确性可能不会改变,但位置会改变(用户正在移动)。如果你只是显示一个列表,你应该记住以前的结果。获取新结果,如果它们不同,请更新您的列表。
You should update the view every time you get a (more accurate) location. So remember the last location and check whether
horizontalAccuracy
/verticalAccuracy
have decreased (smaller = more accurate, but negative = invalid). Maybe even updating every time you get a new location would be better since the accuracy might not change but the position does (user is moving).If you just show a list, you should remember the previous results. Fetch the new result and if they differ, update your list.
当 CLLocationManager 从核心位置系统获取更新的命中时,它会触发其委托的
didUpdateToLocation:fromLocation:
方法。在该方法中,您希望在获得更新的位置时执行任何操作。在您的情况下,您需要检查新位置的
.horizontalAccuracy
属性以查看它是否是高质量结果。您可能还想放弃前几个,因为 CL 会将上次位置会话的最后一个命中作为新位置会话的第一个命中。无论哪种方式,这都是您想要获取数据的方法。
When CLLocationManager gets an updated hit from the Core Location system, it fires its delegate's
didUpdateToLocation:fromLocation:
method. Inside that method, you want to do whatever you do when you get an updated location.In your case, you'll want to check that new location's
.horizontalAccuracy
property to see that it's a high quality result. You also probably want to chuck the first few, because CL will give you the last hit of the last location session as your first hit of the new one.Either way, that's the method where you want to do your fetching of data.
没有一个位置,您可以从 CL 获取一系列位置,其中一个是缓存的,然后其他位置的精度会随着时间的推移而变化,精度会变得更好(或更差),所需时间从 1 秒到 10 分钟不等。
CL 首先会为您提供一个缓存位置,这可能足以满足您的目的。如果它不是太旧(<60 秒)并且精度合理,请继续使用缓存的位置,这将是快速结果。否则,您必须等待足够好的准确度(您必须决定什么是足够的,请查看
.horizontalAccuracy
)。如果您坚持在调用 viewDidLoad 之前获得位置(viewWillAppear 会是一个更好的地方),那么您必须推迟推送该 viewController,直到您获得足够的位置。如何执行此操作取决于视图控制器的加载方式。
There is no one location, you get a series of locations from CL, one cached and then others varying in accuracy over time, getting better (or worse) accuracy, taking anywhere from 1 second to 10 minutes.
CL will start by giving you a cached location, which may be good enough for your purposes. If it's not too old (<60 seconds) and reasonable accuracy, go ahead and use the cached location, it will be the fasted result. Otherwise you have to wait for a sufficiently good accuracy (you have to decide what is sufficient, look at
.horizontalAccuracy
).If you insist on having the location before viewDidLoad is called (viewWillAppear would be a better place) then you have to hold off pushing that viewController until you have a sufficient location. How you do that depends on how that view controller is being loaded.
这就是我要做的。加载您的视图,然后在 viewDidAppear 中调用您的方法来更新位置。我一直在 github 上使用一个不错的开源项目,名为 MBProgressHUD。当它完成工作时,它将在屏幕上显示各种进度指示器(这也会阻止用户离开您的视图)。它还向用户提供视觉反馈,表明您的应用程序尚未停止并且正在运行。
您仍然需要解决准确性问题,但进度视图让您有时间加载视图并在提取所需内容时保持用户参与。并保持您的 UI 流畅。
Here is what I would do. Load your view, and in your viewDidAppear, call your method to update location. I have been using a nice open source project on github called MBProgressHUD. It will display a variety of progress indicators on the screen (that will also block the user from leaving your view) while it finishes its work. It also gives a visual feedback to the user that your app hasn't stalled and is working.
You will still need to sort out your accuracy issues, but the progress view gives you time to load the view and keep the user engaged while you pull in what you need. And keeps your UI fluid.