Can we have custom annotation view for the users current location in iOS?
I need to remove the blue dot (with circles) with my own custom view (say some ping pin). Is it possible to do this?
If we do, does this pin move to new location when there is change in user's location? Or do we need to handle it programmatically?
I observed that if we use default blue dot for user's current location, then its gets updated in the map when there is change in user location.
I just want to know if this can be done with our own custom view.
Best Answer
Yes, you can have a custom view for the user's location.
Unfortunately, it's harder to implement than it should be because even though the documentation for the viewForAnnotation delegate method claims that you can just supply your own view if the annotation class is
MKUserLocation
, the custom view does not then continue to move with the user's location. In fact, when a custom view is returned forMKUserLocation
, the map view stops updating the user location entirely (the map view'sdidUpdateUserLocation
delegate method no longer fires). I believe this is a bug.A workaround is to use
CLLocationManager
and a custom annotation...Make sure
showsUserLocation
isNO
or unchecked on the map view.Declare properties for a
CLLocationManager
and a custom annotation using a custom class that implements theMKAnnotation
protocol (or you could just use the genericMKPointAnnotation
class).In
viewDidLoad
or some other appropriate place, create theCLLocationManager
, set itsdelegate
and callstartUpdatingLocation
.In the location manager's
didUpdateToLocation
delegate method (not the map view'sdidUpdateUserLocation
delegate method), create or update your custom annotation:Finally, in the map view's
viewForAnnotation
delegate method, you would return a custom annotation view if the annotation is your custom user location annotation.