Ios7 当我将self.customAnnotation放入循环时,它将不起作用。有人能帮忙吗。我正在使用absolutions.com(异步)的代码

Ios7 当我将self.customAnnotation放入循环时,它将不起作用。有人能帮忙吗。我正在使用absolutions.com(异步)的代码,ios7,annotations,mkannotationview,Ios7,Annotations,Mkannotationview,以下是我的说明: 在循环中时,viewForAnnotation返回nil。 当我将self.customAnnotation alloc行添加到viewDidLoad语句或单独调用它时,不会出现问题,并且annotation=self.customAnnotation。在循环中,注释会松开self.customAnnotation - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <

以下是我的说明: 在循环中时,viewForAnnotation返回nil。 当我将self.customAnnotation alloc行添加到viewDidLoad语句或单独调用它时,不会出现问题,并且annotation=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;
}

安娜的解决方案奏效了


在viewForAnnotation中,您应该使用if[annotation isKindOfClass:[BasicPanNotation class]],而不是if annotation==self.customAnnotation,因为self.customAnnotation一次只指向一个注释,并且不能保证每次添加后都会调用一个注释Anna Jun 6'14 2:22

你能试着更清楚地说明你在问什么吗?在viewForAnnotation中,你应该使用if[annotation isKindOfClass:[BasicPanNotation class]]而不是if annotation==self.customAnnotation因为self.customAnnotation一次只指向一个注释,并且不能保证viewForAnnotation会在每次添加后一次调用一个注释。
- (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];
}