gpt4 book ai didi

ios - map 注释显示所有点的所有相同图像/图钉

转载 作者:行者123 更新时间:2023-11-29 10:53:24 26 4
gpt4 key购买 nike

我有一个条件语句,可以在下面的方法中添加 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;

enter image description here

最佳答案

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

虽然@Ar Ma 是正确的注释 View 的 annotation应该设置属性(以防 View 被重新使用),这不会解决主要问题。

注释 View 的 image是根据 categoryIdNumber 的值设置的这似乎是 viewForAnnotation 之外的一些变量委托(delegate)方法。

不能假设:

  1. viewForAnnotation将在您调用addAnnotation 后立即调用.即使在 iOS 6 或更早版本中,也不能保证这一点。
  2. viewForAnnotation每个注解只会被调用一次。当用户平移或缩放 map 并且注释返回到屏幕上时,可以针对同一注释多次调用委托(delegate)方法。
  3. viewForAnnotation将按照您添加注释的相同顺序调用。这是第 1 点和第 2 点的结果。

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

发生的事情是viewForAnnotation在所有或部分 addAnnotation 之后的某个时间被 map View 调用调用在此时完成categoryIdNumber可能是与最后添加的注释相关的值,所有注释都使用适用于最后一个注释的图像。


要解决此问题(不管 iOS 版本),您必须输入正确的 categoryIdNumber 调用 addAnnotation 之前,每个注释对象中的值.

看起来你的注解类是MyAnnotation你已经有了一个catMapId其中的属性(property)。

您必须在 调用 addAnnotation 的注释中设置此属性-- 不在 viewForAnnotation内部为时已晚的方法。 (顺便说一句,您正在 MyAnnotation 方法中创建一个 viewForAnnotation 对象,这是毫无意义的。)


因此,您在哪里创建和添加注释(不在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com