我有一个条件语句,可以在下面的方法中添加 map 注释图标/图钉。我遇到的问题是 map 上填充了所有相同的图标。它应该检测猫 ID 并根据检测到的猫 ID 显示图标。我不确定问题是什么,因为这在 iOS 6 中确实有效,但现在在 iOS 7 中, map 仅显示所有相同的注释图标图像。
- (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 正确地指出应设置注释 View 的 annotation
属性(以防 View 被重复使用),但这并不能解决主要问题。
注释 View 的image
是根据categoryIdNumber
的值设置的,该值似乎是viewForAnnotation外部的某个变量
委托(delegate)方法。
您不能假设:
viewForAnnotation
将在您调用addAnnotation
后立即调用。即使在 iOS 6 或更早版本中,也无法保证这一点。viewForAnnotation
对于每个注释仅调用一次。当用户平移或缩放 map 并且注释返回到屏幕上时,可以针对同一注释多次调用委托(delegate)方法。viewForAnnotation
将按照您添加注释的顺序调用。这是第 1 点和第 2 点的结果。
我假设在调用addAnnotation
之前,正确设置了categoryIdNumber
,然后基于上述错误的假设,viewForAnnotation
使用categoryIdNumber
设置图像。
发生的情况是,在完成全部或部分 addAnnotation
调用之后, map View 会调用 viewForAnnotation
,此时 categoryIdNumber
code> 可能是与最后添加的注释相关的值,并且所有注释都使用适用于最后一个注释的图像。
要解决此问题(无论的 iOS 版本),您必须将正确的 categoryIdNumber
值放入每个注释对象之前> 调用addAnnotation
。
看起来您的注释类是 MyAnnotation
并且其中已经有 catMapId
属性。
您必须在调用 addAnnotation
之前在注释中设置此属性 - 而不是 内部 viewForAnnotation
方法已经太晚了。 (顺便说一句,您正在 viewForAnnotation
方法内部创建一个 MyAnnotation
对象,这是毫无意义的。)
因此,您在哪里创建和添加注释(不在 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;
}
关于ios - map 注释显示所有点的所有相同图像/图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19514494/