Question

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;
}
Was it helpful?

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;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top