In my original answer, I thought that the goal was to replace the blinking blue dot that you get when you use a MKMapView
with showUserLocation = YES
and userTrackingMode = MKUserTrackingModeFollow
. Hence I showed how to replace it with an image or with a standard pin.
But it turns out that the problem is not that there is a blue dot showing the current location, but rather that the animation of it is getting interrupted and it appears and disappears as the user pans and zooms on the map.
I have seen that behavior if you call removeAnnotations
and remove all annotations (including the system generated MKUserLocation
annotation). I've also seen that behavior if you turn off showUserLocation
and turn it back on.
The OP points out that none of these situations apply, but for future readers, those are a few considerations that might cause this behavior.
Original answer:
The easiest answer is to make sure your controller is the delegate
for your MKMapView
, and then define a viewForAnnotation
that detects a MKUserLocation
, and replace the annotation view with whatever you want. For example, if you had a @"user.png"
image you wanted to show, it might look like:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
NSString *annotationIdentifier = @"userlocation";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView.image = [UIImage imageNamed:@"user.png"];
}
return annotationView;
}
// again, if you had other annotation types, such as MKPointAnnotation,
// handle them here
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
...
}
return nil;
}
Or, if you wanted to show a standard pin, you could:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
NSString *annotationIdentifier = @"userlocation";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
}
return annotationView;
}
}
// again, if you had other annotation types, such as MKPointAnnotation,
// handle them here
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
...
}
return nil;
}