创建:2011.06.02
修改:2012.03.28
iOS5:
*系统的定位功能有2个设置。第一,设置是否在本机禁用定位功能;第二,设置是否允许某程序使用定位功能。
*[CLLocationManager locationServicesEnabled] 用于检测第一种情况。
*[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied用于检测第二种情况。
*也可以通过CLLocationManager的回调,检测第二种情况。
*当用户首次运行程序的定位功能时,如果用户禁用了第一种情况,则会提示用户进入Setting设置,或者Cancel。这个提示只提示一次。
如果选择Cancel,则定位失败,但是却没有调用回调函数。
如果选择Setting,并设置了On,则返回程序时,会自动进入下面的检测。
如果 选择Setting,但是却没有改变什么,重新返回程序,则该提示仍然存在。就好象什么也没发生过。
如果选择Setting,先设置成On,然后又设置成Off,则当重新返回程序时,什么也不提示,定位失败(但是没有回调)。这种行为就相当于选择了Cancel。
*当用户首次运行程序的定位功能时,如果用户没有禁用了第二种情况,则系统会自动弹出一个消息框,询问是否允许本程序的定位功能。
如果是,则正常流程。
如果否,则无法定位。当然,这是可以通过CLLocationManager的回调检测到,而MapKit则无法检测到。
*一个台机器,对于一个程序只提示一次。如果想重新提示,则需要如下设定:Settings >> General >> Reset >>Reset Location warnings
*该提示消息框,是系统监测到第一个定位消息的时候提示的,目前还不能具体定位到是哪个函数引发的。
*两种提示框都可以增加额外的内容,通过,设定
locationManager.purpose = @"test";
可以在Title的下面,加入一个message,它的内容就是上面的@"test"。
*有一个判断GPS是否打开的回调:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
但是这个函数不太好用。如果是第一种情况的Alert,则status先为kCLAuthorizationStatusDenied,然后才正常流程。
这导致程序无法判断GPS是不是真的被完全拒绝了。
还是使用- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
判定好一些。
其它网页上的信息:
转:
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 UIApplicationWillResignActiveN
otification 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.
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.
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.
+++++