1

我有一个 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;
}
4

1 回答 1

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;

}

于 2012-05-02T13:54:32.247 回答