我有一张地图,我在其中添加了几个注释,如下所示:
for (Users *user in mapUsers){
double userlat = [user.llat doubleValue];
double userLong = [user.llong doubleValue];
CLLocationCoordinate2D userCoord = {.latitude = userlat, .longitude = userLong};
MapAnnotationViewController *addAnnotation = [[MapAnnotationViewController alloc] initWithCoordinate:userCoord];
NSString *userName = user.username;
NSString *relationship = user.relationship;
[addAnnotation setTitle:userName];
[addAnnotation setRelationshipParam:relationship];
[self.mainMapView addAnnotation:addAnnotation];
}
使用此委托方法代码:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifier = @"AnnotationIdentifier";
if ([annotation isKindOfClass:[MapAnnotationViewController class]]) {
MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;
if ([sac.relationshipParam isEqualToString:@"paramA"])
{
annView.image = [UIImage imageNamed:@"image1.png"];
}
else if ([sac.relationshipParam isEqualToString:@"paramB"])
{
annView.image = [UIImage imageNamed:@"image2.png"];
}
else if ([sac.relationshipParam isEqualToString:@"paramC"])
{
annView.image = [UIImage imageNamed:@"image3.png"];
}
return annView;
}
else {
return nil;
}
这一切都适用于地图的原始加载。但是,当我选择注释(其自定义代码太长而无法发布但包含放大)时,之前绘制的注释图像已更改图标。在该过程中不会重新绘制地图并且不会重新添加注释。当我在地图上向后捏时,图像是不同的(它们将错误的关系参数与错误的 image1-3.png 匹配。
谁能想到为什么会发生这种情况,或者要寻找什么?