地图注释显示所有点的所有相同图像/引脚

我有一个条件语句来添加地图注释图标/引脚在下面的方法。 我遇到的问题是地图上正在填充所有相同的图标。 它应该检测到的猫ID和显示图标取决于检测到的猫ID。 我不知道是什么问题,因为这在iOS 6中工作,现在在iOS 7中,地图只显示所有相同的注释图标图像。

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation { annView = nil; if(annotation != mapingView.userLocation) { static NSString *defaultPinID = @""; annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if ( annView == nil ) annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; annView.rightCalloutAccessoryView = rightButton; MyAnnotation* annotation= [MyAnnotation new]; annotation.catMapId = categoryIdNumber; NSLog(@"categoryIdNumber %@",categoryIdNumber); NSLog(@"annotation.catMapId %@",annotation.catMapId); if (annotation.catMapId == [NSNumber numberWithInt:9]) { annView.image = [UIImage imageNamed:@"PIN_comprare.png"]; NSLog(@"annview 9"); } else if (annotation.catMapId == [NSNumber numberWithInt:10]) { annView.image = [UIImage imageNamed:@"PIN_mangiare.png"]; NSLog(@"annview 10"); } else if (annotation.catMapId == [NSNumber numberWithInt:11]) { annView.image = [UIImage imageNamed:@"PIN_visitare.png"]; NSLog(@"annview 11"); } else if (annotation.catMapId == [NSNumber numberWithInt:12]) { annView.image = [UIImage imageNamed:@"PIN_vivere.png"]; NSLog(@"annview 12"); } annView.canShowCallout = YES; } return annView; 

}

在这里输入图像说明

如果如你所说,“这在iOS 6中有效”,那么你应该认为它确实(或似乎是) 幸运,并且这种设置注释图像的方法不应该依赖于任何版本。

虽然@Ar Ma是正确的,注解视图的annotation属性应该被设置(如果视图正在被重用),这不会解决主要问题。

注解视图的image是基于categoryIdNumber的值设置的,它似乎是viewForAnnotation委托方法之外的一些variables。

不能假设:

  1. 在调用addAnnotation之后, viewForAnnotation将被立即调用。 即使在iOS 6或更早的版本中,这也不能保证。
  2. viewForAnnotation每个注释只会被调用一次。 可以多次调用委托方法以获得与用户平移相同的注释或放大地图,并将注释返回到屏幕上。
  3. viewForAnnotation将以与添加注释相同的顺序进行调用。 这是第1点和第2点的结果。

我假设就在你调用addAnnotation之前, categoryIdNumber被设置正确,然后基于上述不正确的假设, viewForAnnotation使用categoryIdNumber来设置图像。

现在发生的事情是, viewForAnnotation被地图视图调用,或者一些addAnnotation调用完成,在这一点上, categoryIdNumber可能是与最后一个注释相关的值,所有的注释都使用适用于最后一个注释的图像。

为了解决这个问题( 无论 iOS版本如何),您必须调用addAnnotation 之前将正确的categoryIdNumber值放入每个注释对象中。

它看起来像你的注释类是MyAnnotation ,你已经有了一个catMapId属性。

您必须调用addAnnotation 之前在注释中设置此属性 – 不在viewForAnnotation方法 ,这太迟了。 (顺便说一下,你正在viewForAnnotation方法里面创build一个毫无意义的MyAnnotation对象。)

所以在你创build和添加注释的地方(不在viewForAnnotation ):

 MyAnnotation* myAnn = [[MyAnnotation alloc] init]; myAnn.coordinate = ... myAnn.title = ... myAnn.catMapId = categoryIdNumber; // <-- set catMapId BEFORE addAnnotation [mapView addAnnotation:myAnn]; 

那么viewForAnnotation的代码应该是这样的:

 - (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation { annView = nil; if(annotation != mapingView.userLocation) { static NSString *defaultPinID = @"MyAnnId"; annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if ( annView == nil ) { annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; annView.canShowCallout = YES; } else { //view is being re-used, re-set annotation to current... annView.annotation = annotation; } UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; annView.rightCalloutAccessoryView = rightButton; //Make sure we have a MyAnnotation-type annotation if ([annotation isKindOfClass:[MyAnnotation class]]) { //Do not CREATE a local MyAnnotation object here. //Instead, get the catMapId from the annotation object //that was PASSED INTO the delegate method. //MyAnnotation* annotation= [MyAnnotation new]; //annotation.catMapId = categoryIdNumber; MyAnnotation *myAnn = (MyAnnotation *)annotation; //The value of the external variable categoryIdNumber is irrelevant here. //NSLog(@"categoryIdNumber %@",categoryIdNumber); NSLog(@"myAnn.catMapId %@",myAnn.catMapId); //Put the NSNumber value into an int to simplify the code below. int myAnnCatMapId = [myAnn.catMapId intValue]; NSString *imageName = nil; switch (myAnnCatMapId) { case 9: { imageName = @"PIN_comprare.png"; break; } case 10: { imageName = @"PIN_mangiare.png"; break; } case 11: { imageName = @"PIN_mangiare.png"; break; } case 12: { imageName = @"PIN_vivere.png"; break; } default: { //set some default image for unknown cat ids... imageName = @"default.png"; break; } } annView.image = [UIImage imageNamed:imageName]; NSLog(@"annview %d", myAnnCatMapId); } } return annView; } 

最后加上这一行:

 annView.annotation = annotation; 

同样的麻烦,对我来说,决定是不使用的

 pinView.animatesDrop = YES; 

自定义图标只是不适合我的animation下降。