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.
Asked
Active
Viewed 2.9k times
20
-
3i tried by adding the details appending to subtitle.. But it showed in one line.. How i can show details in multiple lines – S.P. Feb 26 '10 at 13:59
1 Answers
15
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

Adrian P
- 6,479
- 4
- 38
- 55

Ryan Ferretti
- 2,891
- 2
- 27
- 37
-
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 – S.P. Feb 26 '10 at 22:27
-
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 Feb 27 '10 at 16:15
-
2the tutorial only changes the annotation image.. that's pretty much a one-liner – MJB May 21 '12 at 15:14
-
-
@S.P. see this link http://stackoverflow.com/questions/5831382/how-to-display-2-lines-of-text-for-subtitle-of-mkannotation-and-change-the-image – balkaran singh Sep 09 '16 at 05:18