ios - MKMapview注解动态pin图缩放后变化

标签 ios mapkit mkannotationview

我正在做一个在 map 上显示 7 种不同类型注释的小项目。我的注释取自数组中的 url 结果,我使用 JSON 解析它。我有很多注释, map 加载后一切看起来都很好。放大和缩小后,pin 图像由于某种原因变为错误的 pin 图像(特定图像,不知道为什么)。

我确定我在这里遗漏了一些东西......请你帮忙:)?

这是我的代码的一部分,如果您需要它,请告诉我:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

static NSString *identifier;

if(_mapView.tag==1){identifier = @"TurbulencePin";}
if(_mapView.tag==2){identifier = @"IcingPin";}
if(_mapView.tag==3){identifier = @"WindsPin";}
if(_mapView.tag==4){identifier = @"TemperaturePin";}
if(_mapView.tag==5){identifier = @"CloudsPin";}
if(_mapView.tag==6){identifier = @"VisibilityPin";}
if(_mapView.tag==7){identifier = @"MultiplePin";}


if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if ([annotation isKindOfClass:[Annotation class]]) {

    CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    annotationView = nil;

    if (annotationView == nil) {

        annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;


        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",identifier]];
        annotationView.image = img;

    }
else
    {

        annotationView.annotation = annotation;

    }


    return annotationView;

}
return nil;

}

更新:

根据其他人的反馈,我修改了图片设置的代码如下:

 Annotation *myCustomAnn = (Annotation *)annotation;
 NSString *imgName = myCustomAnn.imageName;
 UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@Pin.png",imgName]];
 annotationView.image = img;

 return annotationView;

另外,我删除了 annotationView = nil;

但是,我无法将 annotation.m 中的图像名称设置为硬编码值,因为我需要为每个注释显示不同的图钉图像。我确定有解释,但我可以从 mapView:viewForAnnotation: 下的 annotation.m 获得的唯一值是注释坐标(myCustomAnn.coordinate.latitudemyCustomAnn .coordinate.longitude),我不知道如何从 annotation.m 中获取其他属性

其他属性,如标题、imgname 等返回为 null

最佳答案

主要问题是 viewForAnnotation 中的代码依赖于外部变量 _mapView.tag 来确定注释 View 。

假定 map 调用 viewForAnnotation 委托(delegate)方法的时间和频率是不安全的。

如果注释的 View 依赖于某些值,通常最好将这些值直接嵌入注释对象本身。这样,在 viewForAnnotation 委托(delegate)方法中,您可以通过传递给该方法的 annotation 参数引用那些特定于注释的值。这些特定于注释的值应在创建注释时设置(在调用 addAnnotation 之前)。

有关更多详细信息和示例,请参阅:

另一个问题是代码在调用 dequeueReusableAnnotationViewWithIdentifier 后将 annotationView 设置为 nil,这会破坏出队。


至少在 viewForAnnotation 中,处理 Annotation 类的更正代码可能看起来像这样(不是整个方法——只是第二个 if< 中的部分):

static NSString *identifier = @"ann";

CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView 
    dequeueReusableAnnotationViewWithIdentifier:identifier];

//annotationView = nil;  // <-- remove this line

if (annotationView == nil) 
{
    annotationView = [[CustomAnnotationView alloc] 
                         initWithAnnotation:annotation 
                         reuseIdentifier:identifier];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
}
else
{
    annotationView.annotation = annotation;
}

//MOVE the setting of the image to AFTER the dequeue/create 
//because the image property of the annotation view 
//is based on the annotation and we can update image in one place
//after both the dequeue and create are done...

//Get the image name from the annotation ITSELF 
//from a custom property (that you add/set)
Annotation *myCustomAnn = (Annotation *)annotation;

NSString *imgName = myCustomAnn.imageName;  
//imageName is the custom property you added/set

UIImage *img = [UIImage imageNamed:
                   [NSString stringWithFormat:@"%@.png",imgName]];

annotationView.image = img;

return annotationView;

关于ios - MKMapview注解动态pin图缩放后变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491869/

相关文章:

ios - MapKit 打印注释到 map

ios - 以编程方式将UILabel文本对齐到底部?

iphone - 如何按 NSString 长度对 NSMutableArray 进行排序?

objective-c - 直线上点的 Mapkit 坐标

ios - 从 MKMapView 中找出你所在的区域类型

ios - MKMapView iOS 自定义用户位置注释不显示/更新位置

ios - MKViewAnnotation 框架的问题

ios - 点击 userLocation 的标题

ios - Realm 迁移失败,错误为 : Migration Required, 或对象已使用不同的架构版本打开

ios - 使用 AFNetworking setImageWithURL : 设置 UIImageView 圆角