In viewForAnnotation
, the code is creating an MKAnnotationView
but not setting an image
for it. There is no default image
on an MKAnnotationView
so the annotations are invisible.
When you don't implement the delegate at all, the map view creates MKPinAnnotationView
s for you with a red pin color. MKPinAnnotationView
is a convenient subclass of MKAnnotationView
which supplies a pin image (in one of three colors).
When you implement the delegate, it's up to you to create the right view and set the properties as needed.
Either create an MKPinAnnotationView
instead (which provides a default pin image) or set the image
property on the plain MKAnnotationView
.
To use MKPinAnnotationView
:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;
static NSString *defaultPinID = @"identifier";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
{
NSLog(@"Inside IF");
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.pinColor = MKPinAnnotationColorRed; //or Green or Purple
pinView.enabled = YES;
pinView.canShowCallout = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Accessoryview for the annotation view in ios.
pinView.rightCalloutAccessoryView = btn;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
or to use MKAnnotationView
and your own image
:
//same code as the current but add this line
//after the initWithAnnotation:
pinView.image = [UIImage imageNamed:@"SomeImage.png"];