To create a custom annotation view (your replacement for the standard pin), you can just set the image
property of the MKAnnotationView
in the viewForAnnotation
method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
else if ([annotation isKindOfClass:[YourAnnotationClassHere class]]) // use whatever annotation class you used when creating the annotation
{
static NSString * const identifier = @"MyCustomAnnotation";
MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
annotationView.canShowCallout = NO; // set to YES if using customized rendition of standard callout; set to NO if creating your own callout from scratch
annotationView.image = [UIImage imageNamed:@"your-image-here.png"];
return annotationView;
}
return nil;
}
You might also want to adjust the centerOffset
property to get the pin to line up precisely the way you want.
Regarding the customization of the callout, the easiest approach is to specify leftCalloutAccessoryView
, rightCalloutAccessoryView
and/or detailCalloutAccessoryView
. This gives you a surprising degree of control, adding all sorts of images, labels, etc.
If you want to do a radical redesign of the callout, you can have viewForAnnotation
set canShowCallout
to NO
and then respond to setSelected
in your custom annotation view to show your own callout. While in Swift, see Customize MKAnnotation Callout View? for a few options for customizing the callouts.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…