MKMapView放大后,MKPinAnnotationView丢失pinColor [英] MKPinAnnotationView loses pinColor after MKMapView zoom in
问题描述
我有一个MKMapView,它具有许多从解析器xml定义的注释引脚;多数民众赞成在我的代码:
I have a MKMapView with a lot of annotation pins defined from a parser xml; thats my code:
-(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时,我可以看到标题注释,字幕注释以及所有具有不同颜色的图钉.
All right, when my MKMapView is loaded, I can see title annotations, subtitle annotations and all the pins with different colours.
但是,如果我在某个级别上滚动并放大地图,然后再次缩小,则所有图钉都会变为紫色.那里发生了什么事?
But if I scroll and zoom IN the map at a certain level, then zoom OUT again, all the pins become PURPLE. What's happening there?
我也尝试过在两种方法中使用相同的注释"(id)(而不是注释"和"annotation2"),但是我没有结果.
I have tried also using same "annotation" (id) in the two methods (and not "annotation" and "annotation2"), but I have no result.
有没有办法避免这种情况,并在地图滚动和缩放后保留pinColors?
Is there a way to avoid that and keep pinColors after map scroll and zoom?
推荐答案
对于每个注释,不一定都只调用一次viewForAnnotation
委托方法,也不一定要按照添加注释的顺序来调用它.如果将showsUserLocation
设置为YES
,也会调用用户位置(蓝点).
The viewForAnnotation
delegate method isn't necessarily called only once for each annotation nor is it guaranteed to be called in the order that you add the annotations. It's also called for the user location (blue dot) if you set showsUserLocation
to YES
.
缩放或平移地图时,地图视图将再次调用委托方法,因为注释会重新显示.那时,您的myValue
与地图请求查看的注释无关.
When you zoom or pan the map, the map view will call the delegate method (again) as annotations come back into view. At that time, your myValue
will have no relevance to the annotation the map is requesting a view for.
添加myValue
作为注释类的属性,而不是使用类级ivar,并在调用addAnnotation
之前将其与title
和subtitle
一起设置:
Instead of using a class-level ivar, add myValue
as a property of your annotation class and set it along with the title
and subtitle
before you call addAnnotation
:
annotation.title = // NSString from RXML parser
annotation.subtitle = // NSString from RXML parser
annotation.myValue = // float value from RXML parser
^^^^^^^^^^^
然后在viewForAnnotation
中,使用annotation
参数中的myValue
属性而不是ivar.这样,委托方法总是使用特定于其为视图请求的注释的信息:
Then in viewForAnnotation
, use the myValue
property from the annotation
parameter instead of an ivar. This way, the delegate method is always using information specific to the annotation it is requesting a view for:
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
.
Changing the name of the annotation
parameter to annotation2
is not necessary.
无关,但是您应该通过使用dequeueReusableAnnotationViewWithIdentifier
(在SDK或SO中搜索)来实现注释视图的重用.如果注释很多,它可以提高性能.
Unrelated but you should be implementing annotation view re-use by using dequeueReusableAnnotationViewWithIdentifier
(search for it in the SDK or on SO). It can help with performance if there are a lot of annotations.
您的
MyAnnotation
类应如下所示:
Your
MyAnnotation
class should look like this:
@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
添加注释的位置应如下所示:
The place where you add the annotation should look like this:
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
就是原始ViewController
中的myValue
.
此外,在viewForAnnotation
中,将annotation2
改回annotation
,并且不要忘记将MKPinAnnotationView *pinView =[[MKPinAnnotationView alloc] init...
放在MyAnnotation *myPin = ...
行之前.
Also, in viewForAnnotation
, change annotation2
back to annotation
and don't forget to put MKPinAnnotationView *pinView =[[MKPinAnnotationView alloc] init...
before the MyAnnotation *myPin = ...
line.
这篇关于MKMapView放大后,MKPinAnnotationView丢失pinColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!