views:

970

answers:

3

I have an MKMapView in my app and have a button that turns on showsUserLocation for the map, while the map is finding the location I display a spinner so the user knows its working. I know when to stop the spinner by adding NSKeyValueObserver to the map's userLocation property. However if the user has Location Services disabled then the spinner just keeps on going forever but nothing happens, is there any way to know when location services are disabled while using an MKMapView?

+4  A: 

Use locationServicesEnabled, a boolean property of a CLLocationManager.

The MapKit support for automatically showing the user's location is pretty poor. The app I'm writing shows the user's location, but I use CLLocationManager directly and add my own annotation to the map.

mythogen
Thanks, missed that property somehow. Is there anyway to know when the user gets the prompt asking them to re-enable location services? The way the maps app does it is they display the spinner specifically until the user chooses no on that dialog.
ameir
You can find out when you've been denied access, and you can find out if when you've been given access. Off the top of my head, implement the didFailWithError CLLocationManagerDelegate callback to be informed when you are rejected, and use a KVO watch on the locationServicesEnabled variable to find out if it switches to YES. There might be a better way to do it, but that's one way.
mythogen
A: 

mythogen,

I'm beginning to think I am going to have to do the same thing.

My problem is that turning on showsUserLocation causes the MKMapView to turn on the GPS and request constant updates.

What I really want is to display a blue dot for the last known user location, then query core location for the location myself, on regular intervals. That way my app could reduce the power requirements.

How far did you go in adding your own annotation? The standard annotation you get with showsUserLocation has some nice features like the pulsating circle, and the "ring of confusion" to show the error radius for the GPS results based on the accuracy reading.

I wish Apple would allow us to decouple displaying this annotation from updating the GPS location.

Duncan C

Duncan C
A: 

Note that locationServicesEnabled is new in 4.0, so won't work if your deployment target is lower.

Edit: Nevermind -- the class method [CLLocationManager locationServicesEnabled] is new in 4.0. But there is a now deprecated instance property .locationServicesEnabled for earlier SDK's.

Pmatt