Search code examples
iosmkmapviewmkannotationcallouts

MKMapView shows same title for multiple MKPlacemarks


I have a mkmapview that I'm dropping several placemark pins on, however I've not been able to get the pins to show the correct title on the callouts, seems to randomly show a title from the collection of pins on the map. Any ideas? Code looks like:

(void)viewDidLoad {
    [super viewDidLoad];

    [mapView setDelegate:self];

    CLLocationCoordinate2D geos = CLLocationCoordinate2DMake([putInLat doubleValue], [putInLong doubleValue]);
    aMarker = [[RNPlaceMark alloc] initWithCoordinate:geos Title:@"Location A"];

    CLLocationCoordinate2D geos2 = CLLocationCoordinate2DMake([takeOutLat doubleValue], [takeOutLong doubleValue]);
    bMarker = [[RNPlaceMark alloc] initWithCoordinate:geos2 Title:@"Location B"];

    NSArray *annots = [[NSArray alloc] initWithObjects:putInMarker, takeOutMarker, nil];
    [mapView addAnnotations:annots];

}

and

(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSString *title = annotation.title;
    MKPinAnnotationView *pinView=(MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:title];

    if(pinView==nil)
        pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:title] autorelease];

    if(annotation == aMarker)
        [pinView setPinColor:MKPinAnnotationColorGreen];
    else if(annotation == bMarker)
        [pinView setPinColor:MKPinAnnotationColorRed];

    pinView.canShowCallout=YES;
    pinView.animatesDrop=YES;


    return pinView;
}

Solution

  • I switched the code to use MKPointAnnotation it worked fine, so now it looks like...

    I'm executing the following code in my viewDidLoad method on the view that host the UIMapVIew:

    MKPointAnnotation *myMarker = [[MKPointAnnotation alloc] init];
    [myMarker setTitle:@"Hello World"];
    CLLocationCoordinate2D geos = CLLocationCoordinate2DMake([myMarkerLat doubleValue], [myMarkerLong doubleValue]);
    [myMarker setCoordinate:geos];
    
    NSArray *annots = [[NSArray alloc] initWithObjects:myMarker, nil];
    [mapView addAnnotations:annots];
    

    then I have...

    - (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id
                                                                      <MKAnnotation>)annotation
    {
        NSString *title = annotation.title;
        MKPinAnnotationView *pinView=(MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:title];
    
    if(pinView==nil)
        pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:title] autorelease];
    
    //If you want to change the color of the pin you can with something like...
    //if(annotation == whatEverInstanceOfAMarkerIWantToKeep)
    //    [pinView setPinColor:MKPinAnnotationColorGreen];
    
    pinView.canShowCallout=YES;
    pinView.animatesDrop=YES;
    
    
    return pinView;
    

    }