缩放后MKMapview注解dynamicpin图像发生变化
我正在研究一个在地图上显示7种不同types的注释的小项目。 我的注释取自数组中的url结果,并使用JSONparsing。 我有很多的注释,一旦地图加载,一切似乎都很好看。 在放大和缩小之后,针图像由于某种原因而改变为错误的针图像(具体图像,不知道为什么)。
我相信我在这里失去了一些东西…你可以请帮助:)?
这里是我的代码的一部分,让我知道如果你需要它了:
-(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中的图像名称设置为硬编码值,因为我需要为每个注释显示不同的图钉图像。 我敢肯定,有一个解释,但唯一的价值,我可以从annotation.m下的mapView:viewForAnnotation:是注释坐标( myCustomAnn.coordinate.latitude
和myCustomAnn.coordinate.longitude)
,我没有任何线索如何从annotation.m中获取其他属性
其他属性,如标题,imgname等返回为空
一个问题是viewForAnnotation
正在根据一个类实例variables来确定显示正确的图像。 通常,注释图像的标识符将是自定义注释本身的属性,而不是某个外部实例variables。
最重要的是,在所有的注解属性被设置之前,注解似乎被添加到了地图中。 应该推迟addAnnotation
直到所有的注解属性都被设置。
或者,您可以将注释添加到NSMutableArray
,按照您认为合适的方式对其进行调整,并且只使用addAnnotations
(注意s)在最后添加注释,并将其传递给数组。
主要的问题是viewForAnnotation
中的代码依赖于外部variables_mapView.tag
来确定注释视图。
假设地图调用viewForAnnotation
委托方法的时间和频率是不安全的。
如果注释的视图依赖于某些值,通常最好将这些值直接embedded到注释对象本身中。 这样,在viewForAnnotation
委托方法中,可以通过传递给方法的annotation
参数引用那些特定于annotation
值。 在创build注释时(在调用addAnnotation
之前)应该设置这些特定于注释的值。
有关更多详细信息和示例,请参阅:
- 用不同的针脚颜色映射视图注释
- 放大/缩小时MKAnnotationView错误更改了引脚图像
一个单独的问题是代码是在调用dequeueReusableAnnotationViewWithIdentifier
来调用dequeue之后,将annotationView
设置为nil
。
至less在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;
罗布解决
问题是,我无法检索我在VC中设置的注释属性。 原因是我使用了[_mapView addAnnotation:ann];
命令,然后设置所有注释的属性。 也就是说,通过将这条线移动到注释初始属性设置的最后部分,解决了这个问题。
谢谢大家,非常感谢Rob! 我面临着下一个挑战。