地图注释显示所有点的所有相同图像/引脚 [英] Map annotation display all the same image/pins for all points
问题描述
我有一个条件语句,可以在下面的方法中添加地图注释图标/图钉。我遇到的问题是地图上填充了所有相同的图标。它应该检测cat id并根据检测到的cat id显示图标。我不确定问题是什么,因为它在iOS 6中工作,现在在iOS 7中,地图只显示所有相同的注释图标图像。
I have a conditional statement to add map annotation icons/pins in the method below. The problem I'm having is that the map is being populated with all the same icon. It should detect the cat id and display the icons depending on which cat id is detected. I'm not sure what the problem is because this did work in iOS 6 and now in iOS 7 the map only displays all the same annotation icon images.
- (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中工作,你应该认为它相当幸运它(或似乎)并且这种设置注释图像的方法不应该依赖于任何版本。
If, as you say, "this did work in iOS 6", you should consider it quite fortunate that it did (or seemed to) and this approach of setting the annotation's image should not be relied on under any version.
虽然@Ar Ma是正确的,但是应该设置注释视图的注释
属性(如果是视图正在重复使用),这不会解决主要问题。
Although @Ar Ma is correct that the annotation view's annotation
property should be set (in case the view is being re-used), that won't solve the main issue.
注释视图的图像
是基于 categoryIdNumber
的值设置,这似乎是一些变量在之外的 viewForAnnotation
委托方法。
The annotation view's image
is set based on the value of categoryIdNumber
which seems to be some variable outside the viewForAnnotation
delegate method.
你 不能 假设:
- addAnno后,将立即调用>
-
viewForAnnotation
塔季翁。即使在iOS 6或更早版本中,也无法保证这一点。 -
viewForAnnotation
将仅为每个注释调用一次。委托方法可以多次调用相同的注释,因为用户平移或缩放地图并且注释返回到屏幕上。 -
viewForAnnotation
将按照添加注释的顺序调用。这是第1点和第2点的结果。
viewForAnnotation
will be called immediately after you calladdAnnotation
. Even in iOS 6 or earlier, this is not guaranteed.viewForAnnotation
will be called only once for each annotation. The delegate method can be called multiple times for the same annotation as the user pans or zooms the map and the annotation comes back onto the screen.viewForAnnotation
will be called in the same order that you add the annotations. This is a result of points 1 and 2.
我假设在您调用 addAnnotation $之前c $ c>,
categoryIdNumber
设置正确,然后根据上述错误假设, viewForAnnotation
使用 categoryIdNumber
设置图像。
I assume that just before you call addAnnotation
, the categoryIdNumber
is set correctly and then based on the above incorrect assumptions, viewForAnnotation
uses categoryIdNumber
to set the image.
发生的事情是 viewForAnnotation
在完成所有或部分 addAnnotation
调用后,地图视图会调用 categoryIdNumber
与添加的最后一个注释相关联的值和所有注释使用适用于最后一个注释的图像。
What is happening is that viewForAnnotation
is being called by the map view sometime after all or some of the addAnnotation
calls are done at which point categoryIdNumber
is probably the value connected with the last annotation added and all the annotations use the image applicable to the last annotation.
要解决此问题(无论 iOS版本如何),您必须将正确的 categoryIdNumber
值放入每个注释对象 之前 调用 addAnnotation
。
To fix this (regardless of the iOS version), you must put the correct categoryIdNumber
value into each annotation object before calling addAnnotation
.
看起来你的注释类是 MyAnnotation
并且您已经拥有 catMapId
属性。
It looks like your annotation class is MyAnnotation
and you already have a catMapId
property in it.
您必须在 注释中设置此属性 调用 addAnnotation
- 不 viewForAnnotation
方法,为时已晚。 (顺便说一下,你在 viewForAnnotation
方法中创建 MyAnnotation
对象,这是毫无意义。)
You must set this property in the annotation before calling addAnnotation
-- not inside the viewForAnnotation
method which is too late. (By the way, you are creating a MyAnnotation
object inside the viewForAnnotation
method which is pointless.)
所以你在哪里创建和添加注释(不在 viewForAnnotation
):
MyAnnotation* myAnn = [[MyAnnotation alloc] init];
myAnn.coordinate = ...
myAnn.title = ...
myAnn.catMapId = categoryIdNumber; // <-- set catMapId BEFORE addAnnotation
[mapView addAnnotation:myAnn];
然后 viewForAnnotation
中的代码应该像这个:
Then the code in viewForAnnotation
should be like this:
- (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;
}
这篇关于地图注释显示所有点的所有相同图像/引脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!