地图区域更改后,地图标注会显示错误的图片

时间:2015-10-19 05:50:33

标签: ios objective-c mkmapview mkannotation mkannotationview

您好我是mapkit的新手,我创建了一个mapView,我在其中显示注释,为人们提供位置以及名称和照片。在每个注释上都有一个公开按钮,可以按下该按钮以切换到该人员详细信息屏幕。当我从数组中加载人员时,我将MKAnnotation子类化并添加了一个标识符属性,这样当按下注释时,我可以识别原始数组元素并转到正确的人物屏幕。在标注上有人的图像,该图像也通过标识符来自阵列。这种节奏很有效,但是我看到当我移动地图然后回到原始位置时,人们的照片不再与人们的名字同步。 我在想我的viewForAnnotation方法可能有错误,如果有人能指出我在这里缺少的东西,我会很感激。 这是方法代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MyAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"Tch4"];
            //pinView.calloutOffset = CGPointMake(0, 32);
            pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            MyAnnotation *myAnn = (MyAnnotation *) annotation;
            // GET ARRAY IDENTIFIER
            identInt = [myAnn.identifier intValue];

            NSLog(@" Tag = %i", identInt);

            // Load and display photo using SDWEBImage
            UIImageView *photoImageView = [[UIImageView alloc] init];
            NSString *urlPhotoId = [jsonTeach [identInt]valueForKeyPath:@"picture.id"];
            NSString *urlPhoto = [NSString stringWithFormat:@"http://soon.nextdoorteacher.com/img/profiles/%@.jpg", urlPhotoId];

            [photoImageView sd_setImageWithURL:[NSURL URLWithString:urlPhoto] placeholderImage:[UIImage imageNamed:@"mortarboard2"]];

            photoImageView.frame = CGRectMake(0,0,50,50);
            pinView.leftCalloutAccessoryView = photoImageView;

        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    return nil;

}

MyAnnotation是包含identifer属性的子类 感谢

1 个答案:

答案 0 :(得分:0)

更改方法,检查视图并同时将自定义图像和数据添加到图钉注释视图中。