当我将self.customAnnotation放在循环中时,它将无法工作。有人可以帮忙吗我正在使用absolutions.com的代码(异步)

时间:2014-06-06 00:50:02

标签: ios7 annotations mkannotationview

这是我的viewForAnnotation: 在循环中,viewForAnnotation返回nil。 当我将self.customAnnotation(alloc)行添加到viewDidLoad语句或单独调用它时,没有问题和注释= self.customAnnotation。在循环中,注释会丢失self.customAnnotation。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id       <MKAnnotation>)annotation {
NSLog(@"viewForAnnotation");
    //if (annotation == self.mapView.userLocation) return nil;
    if (annotation == self.calloutAnnotation) {
        CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
        if (!calloutMapAnnotationView) {
            calloutMapAnnotationView = [[AccessorizedCalloutMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CalloutAnnotation"];
            calloutMapAnnotationView.contentHeight = 78.0f;
            UIImage *asynchronyLogo = [UIImage imageNamed:@"asynchrony-logo-small.png"];
            UIImageView *asynchronyLogoView = [[UIImageView alloc] initWithImage:asynchronyLogo];
            asynchronyLogoView.frame = CGRectMake(5,2,asynchronyLogoView.frame.size.width,asynchronyLogoView.frame.size.height);
            [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
        }
        calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
        calloutMapAnnotationView.mapView = self.mapView;
        return calloutMapAnnotationView;
    } else if (annotation == self.customAnnotation) {
        MKPinAnnotationView *annotationView = [[BasicMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"];

        annotationView.canShowCallout = NO;
        annotationView.pinColor = MKPinAnnotationColorGreen;
        return annotationView;
    }
    return nil;
}

以下是分配给操作按钮的实际循环:

- (IBAction)drawMap:(id)sender {
    NSURL *url = [NSURL URLWithString:@"myurl"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                     options:0
                                                       error:&error];
    if (error != nil)
    {
        // handle the error as you want
    }
    //CLLocationCoordinate2D location;
    //MKPointAnnotation *newAnnotation;
    NSMutableArray *newAnnotations = [NSMutableArray array];
    for (NSDictionary *dictionary in array)
    {
        self.customAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[dictionary[@"latitude"] doubleValue] andLongitude:[dictionary[@"longitude"] doubleValue]];
        [newAnnotations addObject:self.customAnnotation];
    }

    [self.mapView addAnnotations:newAnnotations];
    [self zoomToFitMapAnnotations:self.mapView];
}

1 个答案:

答案 0 :(得分:0)

Anna的解决方案奏效了!

在viewForAnnotation中,不应使用if(annotation == self.customAnnotation),而应使用if([annotation isKindOfClass:[BasicMapAnnotation class]]),因为self.customAnnotation一次只指向一个注释,并且&#39;不保证每次添加后一次调用一个viewForAnnotation。 - Anna Jun 6&#39; 14 at 2:22