MKMapView显示多个MKPlacemarks的相同标题

时间:2012-01-28 03:43:21

标签: ios mkmapview mkannotation callouts

我有一个mkmapview,我正在删除几个地标引脚,但是我无法让引脚在标注上显示正确的标题,似乎随机显示来自引脚上的引脚集合的标题地图。有任何想法吗?代码如下:

(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];

}

(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;
}

1 个答案:

答案 0 :(得分:2)

我切换代码使用MKPointAnnotation它工作正常,所以现在它看起来像......

我正在我托管UIMapVIew的视图的viewDidLoad方法中执行以下代码:

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];

然后我......

- (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;

}