views:

1887

answers:

1

I want to add more deatils 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. So how i can add more details in map.Plz help me..Thanks in advance

+2  A: 

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"

Ryan Ferretti
thank u for ur answer. Can i add the details into the default bubble in wanted format , like with newline ? I tried by addding UIlabel to rightCalloutAccessoryView of MKPinAnnotationView. But its not lloking nice.thanks in advance
Sijo
I wouldn't try to do it the way you are trying to do it... Apple has setup annotations so they can be extended with the MKAnnotationView class. Trying to override the rightCalloutAccessoryView in the way you are doing it is not a good way to do it... you are fighting the framework and it is giving you weird results. If you want a custom annotation, the best way to do it is the way I described.
Ryan Ferretti
see my new edit for more info
Ryan Ferretti
Thank u Ryan Ferretti.....
Sijo