Search code examples
ioslocationmkmapviewcllocationmanagermkmapitem

Getting user's current location coordinates: MKMapItem vs CLLocationManager


This is my scenario: I need to get the user's current location punctually in order to show them a set of nearby points of interest in an MKMapView. I don't need to keep track of user's location. I need someone to clarify which the best way to do this should be:

1) ASFAIK, is it possible to get the current location by calling mapItemForCurrentLocation. You get an MKMapItem object, and I think that this call does not need to have the location services enabled, but I'm not sure if it is possible to get the coordinates for the location this way... is it?

2) Start a CLLocationManager and listen for location updates. And then just take the first location received and stop listening.

I need this to work for iOS 7+

Thanks


Solution

  • For an MKMapView object to be showing user location you will have to have requested authorisation for iOS8 (ie using requestWhenInUseAuthorization on a CLLocationManager object).

    MKMapView objects have a didUpdateUserLocation: delegate method you could use receive user location updates, but this may fire off repeatedly until it reaches the accuracy the map requires - you might need to ignore later updates depending on what you're doing.

    Based on your scenario it could be better to use CLLocationManger, then stop requesting updates once you have a fix of required accuracy.