objective-c iPhone GPS 开发 - 提示 + 技巧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/488088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone GPS Development - Tips + Tricks
提问by Keith Fitzgerald
I'm looking for some feedback, blog links, etc that offer some tips and tricks for iPhone GPS development. I've read and understand the API, I have my demo app up and running, etc but I have run into some "quirks" with gps on iphone. For example, it seems that you you will receive a lot less location updates when your iphone enters "power save" mode. I've read that you can combat this by playing music in your application [not sure if this is true].
我正在寻找一些反馈、博客链接等,为 iPhone GPS 开发提供一些技巧和窍门。我已经阅读并理解了 API,我已经启动并运行了我的演示应用程序,等等,但是我在 iphone 上遇到了一些关于 gps 的“怪癖”。例如,当您的 iphone 进入“省电”模式时,您收到的位置更新似乎少得多。我读过您可以通过在应用程序中播放音乐来解决这个问题 [不确定这是否属实]。
It also seems that, terms of making a reasonable pedometer, you should filter out any reading with horizontal accuracies less than 20 meters. Does this sound right?
看起来,制造一个合理的计步器,你应该过滤掉任何水平精度小于 20 米的读数。这听起来对吗?
Is there any kind of checklist out on the web for iphone gps gotchas?
网上有没有关于 iphone gps 陷阱的清单?
Thanks in advance.
提前致谢。
回答by Tom Harrington
I don't know of any specific list, but here are a few of things you should keep in mind:
我不知道任何具体的列表,但这里有一些你应该记住的事情:
- You're not doing GPS lookups. You're doing Core Location lookups. Core Location might or might not be getting its information from GPS. Lots of people still use pre-3g iPhones that don't have GPS, and even on a 3g phone GPS may be unavailable in many cases (if the person is indoors, for example). In those cases the phone will attempt to triangulate based on cell phone towers, and Core Location will return the result. It'll be much less precise than GPS.
- Core Location will cache data. The first reading it provides to your app is likely to be an old reading, which might or might not be accurate, depending on whether the phone has moved. Make sure to check the timestamp of any location and see if it's from before your app started.
- I don't know what you mean by "power save" mode, but if you're thinking of when the screen is locked/off, that does not stop Core Location from running if your app is still running. On the contrary it's easy to run down your phone's battery much more quickly than you'd expect if you lock the phone while an app that uses Core Location is running, because the phone will continue to update the app as new location data is available. You could avoid this in your application by listening for UIApplicationWillResignActiveNotification to detect the screen locking, and UIApplicationDidBecomeActiveNotification to detect unlock.
- Higher precision results will take more time to acquire, because the longer you wait, the better the results, up to a limit. If you decide you need to be within N meters, consider how long the user might have to stand there waiting for the phone to home in on its location.
- 你不是在做 GPS 查找。您正在执行核心位置查找。Core Location 可能会也可能不会从 GPS 获取其信息。许多人仍然使用没有 GPS 的 3g 之前的 iPhone,即使在 3g 手机上,GPS 在很多情况下也可能不可用(例如,如果这个人在室内)。在这些情况下,手机将尝试根据手机信号塔进行三角测量,Core Location 将返回结果。它会比 GPS 精确得多。
- 核心位置将缓存数据。它提供给您的应用程序的第一个读数可能是旧读数,这可能准确也可能不准确,具体取决于手机是否移动。确保检查任何位置的时间戳,看看它是否来自您的应用程序启动之前。
- 我不知道您所说的“省电”模式是什么意思,但是如果您考虑屏幕何时锁定/关闭,那么如果您的应用程序仍在运行,那并不会阻止 Core Location 运行。相反,如果在使用 Core Location 的应用程序运行时锁定手机,很容易比预期的更快耗尽手机电池电量,因为手机会在新的位置数据可用时继续更新应用程序。您可以通过侦听 UIApplicationWillResignActiveNotification 来检测屏幕锁定和 UIApplicationDidBecomeActiveNotification 来检测解锁,从而在您的应用程序中避免这种情况。
- 获得更精确的结果需要更多时间,因为等待的时间越长,结果就越好,达到一定限度。如果您决定需要在 N 米范围内,请考虑用户可能需要站在那里等待手机回到其位置的时间。
Whether 20 meters is accurate enough is something only you can answer, based on how you expect your application to be used. Test the app and see if it works the way you want it to work.
20 米是否足够准确只有您才能回答,具体取决于您希望如何使用您的应用程序。测试应用程序,看看它是否按您希望的方式工作。
回答by Kendall Helmstetter Gelner
Tom had a great answer (I wanted to note especially the usual return of the first value might be an older cached location) but I had a few more things to add:
汤姆有一个很好的答案(我想特别注意第一个值的通常返回可能是较旧的缓存位置)但我还有一些要添加的内容:
- You can tell the location manager to stop and start again if you want to force it to issue you at least one more location update.
- If you were not aware, there are convenience methods in CLLocation to give you the distance between two locations.
- As the docs note, you should implement locationManager:didFailWithError: even though you do not have to (if only to stop the location manager updates for a time).
- Test thoroughly scenarios where the user disallows location updates for your application!
- Very, very infrequently you may get updates out of order (i.e. you get a more accurate location followed by a slightly less accurate one dated earlier)
- The simulator will give you a location update, but will always report your location as Apple HQ.
- 如果您想强制它向您发出至少一个位置更新,您可以告诉位置管理器停止并重新启动。
- 如果您不知道,CLLocation 中有一些方便的方法可以为您提供两个位置之间的距离。
- 正如文档所述,您应该实现 locationManager:didFailWithError: 即使您不必(如果只是为了停止位置管理器更新一段时间)。
- 彻底测试用户不允许应用程序位置更新的场景!
- 非常非常罕见的情况下,您可能会收到无序的更新(即,您会得到一个更准确的位置,然后是一个日期稍早的不太准确的位置)
- 模拟器将为您提供位置更新,但始终将您的位置报告为 Apple HQ。

