在MKMapView放大后,MKPinAnnotationView失去pinColor
我有一个MKMapView有很多从parsing器xml定义的注释引脚; 那是我的代码:
-(IBAction)LoadAnnotation:(id)sender { RXML element ... RXML iterate.... [myMap removeAnnotations:myMap.annotations]; annotation.title = // NSString from RXML parser annotation.subtitle = // NSString from RXML parser myValue = // float value from RXML parser [mymap addAnnotation:annotation]; }
接着
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation2 { MKPinAnnotationView *pinView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation2 reuseIdentifier:@"MyPin"]; if ( myValue > 0 && myValue < 10) { pinView.canShowCallout = YES; pinView.pinColor = MKPinAnnotationColorRed; pinView.animatesDrop=YES; return pinView; } else if ( myValue > 10 && myValue < 20 ) { pinView.canShowCallout = YES; pinView.pinColor = MKPinAnnotationColorGreen; pinView.animatesDrop=YES; return pinView; } pinView.canShowCallout = YES; pinView.pinColor = MKPinAnnotationColorPurple; pinView.animatesDrop=YES; return pinView; }
好吧,当我的MKMapView加载时,我可以看到标题注释,字幕注释和所有不同颜色的引脚。
但是,如果我在一定的水平上滚动和放大地图,然后再次缩小OUT,所有的引脚变成紫色。 那里发生了什么?
我已经尝试在两个方法(而不是“注释”和“annotation2”)中使用相同的“注释”(ID),但我没有结果。
有没有办法避免这种情况,并保持pinColors后地图滚动和缩放?
viewForAnnotation
委托方法不一定只对每个注释调用一次,也不保证按添加注释的顺序调用。 如果您将showsUserLocation
设置为YES
则还需要用户位置(蓝点)。
当您缩放或平移地图时,地图视图会在注释返回到视图中时调用委托方法(再次)。 那时候,你的myValue
和地图正在请求查看的注释没有任何关系。
在调用addAnnotation
之前,不要使用类级别的ivar,而myValue
添加为注释类的属性,并将其与title
和subtitle
一起设置:
annotation.title = // NSString from RXML parser annotation.subtitle = // NSString from RXML parser annotation.myValue = // float value from RXML parser ^^^^^^^^^^^
然后在viewForAnnotation
,使用annotation
参数中的myValue
属性而不是ivar。 这样,委托方法总是使用特定于其请求视图的注释的信息:
if ( ! [annotation isKindOfClass:[MyCustomAnnotationClass class]]) { return nil; //return a default view if not your custom class } //cast annotation to custom class so we can get the custom property... MyCustomAnnotationClass *myPin = (MyCustomAnnotationClass *)annotation; //use the custom property in the annotation instead of ivar... if (myPin.myValue > 0 && myPin.myValue < 10) { ....
将annotation
参数的名称更改为annotation2
是不必要的。
不相关,但是您应该使用dequeueReusableAnnotationViewWithIdentifier
(在SDK中或在SO上search)实现注释视图重用。 如果有很多注释,它可以帮助提高性能。
你的MyAnnotation
类应该是这样的:
@interface MyAnnotation : NSObject <MKAnnotation> { float myValue; } @property (nonatomic, assign) CLLocationCoordinate2D coordinate; //<-- add @property (nonatomic, copy) NSString *title; //<-- add @property (nonatomic, copy) NSString *subtitle; //<-- add @property (nonatomic, assign) float myValue; @end @implementation MyAnnotation @synthesize coordinate; //<-- add @synthesize title; //<-- add @synthesize subtitle; //<-- add @synthesize myValue; @end
添加注释的地方应该如下所示:
MyAnnotation *annotation = [[MyAnnotation alloc] init]; annotation.title = someTitle; // NSString from RXML parser annotation.subtitle = someSubTitle; // NSString from RXML parser annotation.myValue = someValue; // someValue from RXML parser [mapView addAnnotation:annotation];
以上, someValue
是什么myValue
是在您的原始ViewController
。
此外,在viewForAnnotation
,将annotation2
更改回annotation
并且不要忘记在MyAnnotation *myPin = ...
行之前放置MKPinAnnotationView *pinView =[[MKPinAnnotationView alloc] init...
。