Iphone – Showing custom Annotation View on MKMapView

iphonemkannotationviewmkmapview

I am working on a map application. I am done with fetching locations on certain conditions, showing annotation on map view and displaying my custom callout bubble.

My requirement is to display the annotation pin as follow:

  • A Image with rounded corner to display annotation
  • On the right top corner of the annotation there will be a red badge kind of view.
  • Need to access that red badge view and set text in certain methods.

Now I know I can set Image to the annotation view and make it rounded corner as follow:

pinView.image = [UIImage imageNamed:@"annotationImage"];
[pinView.layer setCornerRadius:8.0];
[pinView setClipsToBounds:YES];

Here pinView is an AnnotationView.

Please let me know if I can create a custom view and assign it to pin view so I can get the red badge and set Image as well.

UPDATE

I tried lot more things and got succeed to put the view. But the problem is if I use a MKAnnotationView object on map I am not able to tap it or call gesture recognizer on it. If I use MKPinAnnotationView than I am able to call didSelected method or add a gesture recognizer but I am not able to set a Image on it as it is a Pin Annotation.
Plz let me know if this is possible.

Thanks in advance.

Best Answer

you need to set your Class as MKMapViewDelegate and fill this method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
 CustomMapAnnotationView *annotationView = nil;

 pinView.image = [UIImage imageNamed:@"annotationImage"];
 [pinView.layer setCornerRadius:8.0];
 [pinView setClipsToBounds:YES];

 NSString* identifier = @"CustomMapAnnotation";
 CustomMapAnnotationView *newAnnotationView = (CustomMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

 if(nil == newAnnotationView) {
    newAnnotationView = [[[CustomMapAnnotationView alloc] initWithAnnotation: pinView reuseIdentifier:identifier] autorelease];
 }

 annotationView = newAnnotationView;
 [annotationView setEnabled:YES];
 [annotationView setCanShowCallout:YES];

 return annotationView;
}

Thus, you can return a custom Class, subclass of MKAnnotationView, and you can add badge now to your custom view.

Related Topic