GPS 位置需要几秒钟才能检索
我编写了一个使用 iPhone 相对 GPS 位置的 iPhone 应用程序。
我在用户界面中有一个按钮可以进行反向地理定位(根据纬度和经度拉出街道名称)。 检索到的位置对象
CLLocation *location = [[self locationManager] location];
唯一的问题是,如果用户过早点击按钮,则 为零。我正在 viewDidLoad 中初始化 LocationManager,因为我不想反对创建,除非用户实际加载此屏幕。我也立即开始更新位置...但如果用户加载屏幕并立即点击 GPS 按钮,位置为零。
那么问题来了:你知道 CLLocationManager 检索位置大约需要多少时间吗?
谢谢
I have written an iPhone app that uses the iPhone's relative GPS location.
I have a button in the user interface that does reverse geolocation (pulls the street name bases on the lat and long). The only problem is that the location object as retrieved by
CLLocation *location = [[self locationManager] location];
is nil if the user taps the button too soon. I am initializing the LocationManager in viewDidLoad as I don't want to object to be created unless the user actually loads this screen. I start updating the location straight away as well... but still if the user loads the screen and taps the GPS button straight away the location is nil.
Here comes the question: do you know roughly how much time the CLLocationManager needs to retrieve the location?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
手机可能需要几秒钟的时间才能检索此数据。为了可用性,您可能需要停用按钮控件。找到位置后,触发
NSNotification
。注册您的视图控制器以侦听此通知,这会触发重新激活按钮控件的选择器。It can take the phone a few seconds to retrieve this data. For usability, you may want to deactivate the button control. Fire an
NSNotification
once the location is found. Register your view controller to listen for this notification, which fires a selector that re-activates the button control.获取位置修复取决于您想要的精确度,以及之前是否有某种位置数据、天气如何、您是否在室内/室外……确实没有一个答案。如果您转到地图应用程序并单击“定位”按钮,那么您会注意到,在不同的情况下,启动解决过程需要一段时间。
回调会告诉您何时有数据;您不应该通过轮询或假设即时访问来获取它(或更糟糕的是,阻止应用程序直到您希望找到数据)。听起来这里的关键问题是你的按钮是同步执行请求而不是动态的;换句话说,无论您得出的“n”值是什么,竞争条件都保证在某个时刻失败。
更改代码,使按钮在后台调用位置获取,然后弹出信息或稍后填写表单。
Acquiring a location fix is dependent on the amount of accuracy you want, and whether it's previously had some kind of location data, what the weather is like, whether you're inside/outside ... there really isn't one answer. If you go to the Maps application and click on the 'locate' button then you'll notice that in different circumstances, it will be some time after you start the resolution process.
The callbacks will tell you when there is data; you shouldn't get it by polling or assuming instant access (or worse, blocking the app until you expect to find data). It sounds like the key problem here is your button is synchronously executing the request and not being dynamic; in other words, a race condition guaranteed to fail at some point, regardless of what value of 'n' you come up with.
Change your code so that the button invokes the location acquisition in the background, and then subsequently pops up the information or fills out the form later.