[英]MKMapview annotation dynamic pin image changes after zooming
我正在做一个小项目,在地图上显示7种不同类型的注释。 我的注释来自数组中的url结果,我使用JSON对其进行了解析。 我有很多注释,一旦地图加载,一切看起来都不错。 放大和缩小后,针脚图像由于某种原因变为错误的针脚图像(特定图像,不知道为什么)。
我确定我在这里缺少什么...可以请您帮忙:)吗?
这是我的代码的一部分,如果您需要它,请告诉我:
-(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;
但是,我无法将注释.m中的图像名称设置为硬编码值,因为我需要为每个注释显示不同的图钉图像。 我敢肯定有一个解释,但是从mapView:viewForAnnotation:下的commentment.m可以得到的唯一值是注释坐标( myCustomAnn.coordinate.latitude
和myCustomAnn.coordinate.longitude)
,我不知道如何从annotation.m获取其他属性
其他属性(例如title,imgname等)返回为null
主要问题是viewForAnnotation
中的代码依赖于外部变量_mapView.tag
来确定注释视图。
假设地图将在何时以及以何种频率viewForAnnotation
委托方法是不安全的。
如果注释的视图取决于某些值,通常最好将这些值直接嵌入到注释对象本身中。 这样,在viewForAnnotation
委托方法中,您可以通过传递给方法的annotation
参数来引用那些特定于annotation
值。 在创建注释时(在调用addAnnotation
之前)应设置那些特定于注释的值。
有关更多详细信息和示例,请参见:
一个单独的问题是代码在调用dequeueReusableAnnotationViewWithIdentifier
来取消出dequeueReusableAnnotationViewWithIdentifier
后,将annotationView
dequeueReusableAnnotationViewWithIdentifier
设置为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;
由Rob解决
问题是,我无法检索在VC中设置的注释属性。 原因是我使用了[_mapView addAnnotation:ann];
命令,然后设置所有注释的属性。 就是说,通过将这条线移动到注释初始属性设置的末尾,可以解决该问题。
谢谢大家,非常感谢Rob! 我要迎接下一个挑战。
一个问题是viewForAnnotation
根据类实例变量确定要显示的正确图像。 通常,注释图像的标识符将是自定义注释本身的属性,而不是某些外部实例变量。
最重要的是,似乎在设置所有注释的属性之前,已将注释添加到地图。 应该将addAnnotation
推迟到所有注释的属性都设置好之后。
或者,您可以将注释添加到NSMutableArray
,根据需要进行调整,然后仅使用addAnnotations
(请注意s)在最后添加注释,并将其传递给数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.