MKAnnotationView在标签上显示不同的文本

时间:2015-09-30 09:25:58

标签: ios objective-c mkmapview mkannotationview

我想在每个annotationView上显示不同的文本,但每个注释都会显示相同的值。

以下是我的代码:

    NSMutableArray *strAnnoTitle;
    -(void)callAddAnnotations{
    cnt = 0;
    [_mapView removeAnnotations:[_mapView annotations]];
    for (id obj in arrPropTemp) {

        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake([[[arrPropTemp valueForKey:@"Latitude"] objectAtIndex:cnt] floatValue], [[[arrPropTemp valueForKey:@"Longitude"] objectAtIndex:cnt] floatValue]);

        strAnnoTitle[cnt] = [obj valueForKey:@"ListPriceForMap"];

        // Add an annotation
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = coords;
        [self.mapView addAnnotation:point];

        cnt++;


    }
}


    -(MKAnnotationView *)createAnnotation:(MKAnnotationView *)viewAn{
    UILabel *lbl = (UILabel *)[viewAn viewWithTag:100];
    [lbl setText:strAnnoTitle[cnt]];
    return viewAn;
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *viewAn = [[[NSBundle mainBundle] loadNibNamed:@"MapAnnotation" owner:self options:nil] lastObject];

        viewAn = [self createAnnotation:viewAn];

    return viewAn;
    return nil;
}

输出:

enter image description here

我哪里出错了?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

更改您的实施。

-(void)callAddAnnotations{
    cnt = 0;
    [_mapView removeAnnotations:[_mapView annotations]];
    for (id obj in arrPropTemp) {

        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake([[[arrPropTemp valueForKey:@"Latitude"] objectAtIndex:cnt] floatValue], [[[arrPropTemp valueForKey:@"Longitude"] objectAtIndex:cnt] floatValue]);

        strAnnoTitle[cnt] = [obj valueForKey:@"ListPriceForMap"];

        // Add an annotation
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = coords;
        [point setTitle:strAnnoTitle[cnt]];
        [self.mapView addAnnotation:point];

        cnt++;


    }
}

更改您的创建方法

-(MKAnnotationView *)createAnnotation:(MKAnnotationView *)viewAn{
    UILabel *lbl = (UILabel *)[viewAn viewWithTag:100];
    [lbl setText: viewAn.annotation.title];//can move this also to delegate
    return viewAn;
}

将viewForAnnotation方法更改为这样。

-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *cabAnnotationIdentifier=@"cabAnnotationIdentifier";
MKAnnotationView * viewAn =[MapView dequeueReusableAnnotationViewWithIdentifier:cabAnnotationIdentifier];
if(!annotationView){            
    MKAnnotationView *viewAn = [[[NSBundle mainBundle] loadNibNamed:@"MapAnnotation" owner:self options:nil] lastObject];

    viewAn = [self createAnnotation:viewAn];
}
return viewAn;

}

答案 1 :(得分:0)

获取1个NSObject文件,其中包含您想要注释的必填字段。在mapview上添加注释并附加有关该Nsobject文件的信息,并且与它将工作的viewforannotation方法相同。

1。)NSObject文件 REVPin.h

@interface REVPin : NSObject  <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSInteger tag;
NSArray *nodes;

}

@property(nonatomic, retain) NSArray *nodes;

@property(readwrite, nonatomic) NSInteger tag;

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;

@property(nonatomic, copy) NSString *title;

-(void)AddPinToMap:(NSMutableArray *)PinArray
{
NSMutableArray *pins = [NSMutableArray array];
oldZoomLevel = (int)[self zoomLevelForMapRect:MapView.visibleMapRect withMapViewSizeInPixels:CGSizeMake(MapView.frame.size.width, MapView.frame.size.height)];
if ([isFirstTimeN isEqualToString:@"YES"]) {
    [MapView removeAnnotations:MapView.annotations];
    for(int i=0;i<[PinArray count];i++) {
        CGFloat latitude=[[[PinArray objectAtIndex:i] objectForKey:@"lat"] floatValue];
        CGFloat longitude=[[[PinArray objectAtIndex:i] objectForKey:@"lon"] floatValue];
        CLLocationCoordinate2D newCoord = {latitude, longitude};
        REVPin *pin = [[REVPin alloc] init];
        pin.title = [NSString stringWithFormat:@"Pin %i",i+1];
        pin.subtitle = [NSString stringWithFormat:@"Pin %i subtitle",i+1];
        pin.coordinate = newCoord;
        pin.tag = i;
        pin.userId = [NSString stringWithFormat:@"%@",[[PinArray objectAtIndex:i] objectForKey:@"u"]];
        [pins addObject:pin];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
REVPin *pin = (REVPin *)annotation;
MKAnnotationView *annView;
if([annotation class] == MKUserLocation.class)
{
    return nil;
}
annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                       reuseIdentifier:@"pin"];
[array addObject:pin];
return annView;

    }

尝试使用上述方法,它将起作用