2012-01-28 39 views
1

我有一個mkmapview,我正在放置幾個地標引腳,但是我一直無法獲得顯示正確標題的引腳,似乎隨機顯示標題從地圖上的針腳收集。有任何想法嗎?代碼如下所示:MKMapView顯示多個MKPlacemarks的相同標題

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

回答我自己的問題,我從我的自定義標記對象切換到MKPointAnnotation,並開始工作。 – 2012-01-28 03:55:02

+1

如果需要,您可以將其作爲答案張貼,儘管問題可能出現在RNPlaceMark的init方法中。 – Anna 2012-01-28 04:14:13

回答

2

我切換到使用MKPointAnnotation它工作得很好的代碼,所以現在它看起來像......

我執行下面的代碼在查看我的viewDidLoad方法承載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]; 

那麼我...

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

}

相關問題