17

I want to use a MKMapView to display the user current location using the default breathing blue pin and I want to record the user movement at the same time. Is there any way that I could use the GPS manager (not sure if this is a CLLocationManager) the MKMapView uses when we enabled it to show user location?

I know that I can create my own CLLocationManager. But this feels like adding an overhead to my application and I would like the map and my tracking to remain in sync.

I already explored the following ideas without success:

  • Use the [MKMapView showUserLocation:YES] and add KVO on the userLocation field. This does not work and I am wondering if this is due to the fact that the userLocation field is read only.
  • Use the [MKMapView showUserLocation:YES], create a MKMapViewDelegate and add the tracking when the annotation view for the user location is requested. This does not work, because the annotation view is apparently requested only once???
  • Use a CLLocationManager and try to add the blue pin manually. Unfortunately, I did not find the blue pin in the available pin types, so I tried to create a user annotation manually without success.

Does anyone has any idea how I can achieve this and still benefit from the blue pin or is my only solution to use a CLLocationManager and create my own pin?

Patrice
  • 183
  • 1
  • 6

1 Answers1

18

CLLocationManager uses the same data across all of its instances. MKMapView uses CLLocationManager's data internally. That said the solution to do what you want to do is let MKMapView do its own thing with regards to showUserLocation:. At the same time, create an instance of CLLocationManager and its delegate.

The delegate messages will give you the GPS coordinate location of MKMapView's blue pin. Everything will be in sync with each other.

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
Giao
  • 14,725
  • 2
  • 24
  • 18
  • 1
    Thanks Giao. I guess that "CLLocationManager uses the same data across all of its instances" is what I needed to hear ;) – Patrice Feb 23 '10 at 22:42
  • 3
    Are you sure that CLLocationManager uses the same data across all of its instances? If that was the case, wouldn't Apple have made it a singleton like UIApplication? – ari gold Sep 19 '12 at 03:44
  • 2
    This answer http://stackoverflow.com/a/5930620/1055722 (near the bottom of it) indicates that CLLocationManager does *not* share the same data across all instances. Do you happen to have a reference you can cite? – David K. Hess May 20 '13 at 17:50
  • Cool, that was an answer from a question I posted when I first started with maps stuffs. Looks like things haven't changed much since :) I guess if you use map view, then it might be better if you just stick with it and not use CLLocationManager on top of it. I suppose the values are not too different? – Van Du Tran Sep 27 '13 at 15:49
  • 1
    In my app, I finally used the MKMapView to display the user location, and CLLocationManager to record the locations. I have noticed that the locations differs a little bit between MKMapView and CLLocationManager. – Van Du Tran May 12 '14 at 14:24
  • The `MKMapViewDelegate` protocol has a function: `mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!)`. How does this differ from using a `CLLocationManager`? – Max Aug 23 '15 at 11:20
  • Why there are slight differences between those 2 results like map view and location manager – siva krishna May 31 '16 at 11:21