Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add more details in MKAnnotation in iOS

Tags:

I want to add more details in MKAnnotation like location title, description, date, location name. So it will be four lines that are needed. But I found that only 2 parameters can be passed to MKAnnotation which are title and subtitle. How can I add more details on the map? Plz help me..Thanks in advance.

like image 245
S.P. Avatar asked Feb 26 '10 13:02

S.P.


1 Answers

Take a look at creating a custom MKAnnotationView object... it is basically a UIView that is tailored for map annotations. In that object you could have your 4 custom labels.

In your MKMapViewDelegate class, you implement the viewForAnnotation method:

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

    // determine the type of annotation, and produce the correct type of annotation view for it.
    CustomMapAnnotation* myAnnotation = (CustomMapAnnotation *)annotation;

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

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

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

    return annotationView;
}

And that will display your custom view where ever you have an annotation... if you want a step by step tutorial, check out this video.

hope this helps

EDIT

I actually just found a new Maps example on the apple dev site... has all the source code you need to go through. They are also using a custom MKAnnotationView called WeatherAnnotationView

like image 103
Ryan Ferretti Avatar answered Oct 03 '22 20:10

Ryan Ferretti