在 iphone sdk 地图上添加侦听器(mkannotation 以一种奇怪的方式调用)
我有一个地图标记,我在其中添加了一个事件侦听器。当我点击标记时,我可以让它 NSLog 输出一条消息......但是当我点击地图时,它会执行相同的操作。不知道这是不是正常现象?最后,我试图出现一个弹出视图控制器 - 但这已被搁置,直到它起作用为止。
所以...我对注释视图进行了子类化,如下所示
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.image = [UIImage imageNamed:@"numberplate.png"];
self.frame = CGRectMake(0,0,133,40);
self.centerOffset = CGPointMake(76,0);
//self.selected = NO;
self.canShowCallout = NO;
}
return self;
}
然后在我的主视图控制器中,我有
- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {
VehicleViewInfo *eventView = (VehicleViewInfo *)[lmapView
dequeueReusableAnnotationViewWithIdentifier:
@"eventview"];
if(eventView == nil) {
eventView = [[[VehicleViewInfo alloc] initWithAnnotation:annotation
reuseIdentifier:@"eventview"]
autorelease];
}
[eventView addObserver:self
forKeyPath:@"selected"
options:NSKeyValueObservingOptionNew
context:GMAP_ANNOTATION_SELECTED];
eventView.annotation = annotation;
return eventView;
}
所以然后在我的事件侦听器中,我
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{
NSString *action = (NSString*)context;
NSLog(@"action received: %@", context);
if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){
NSLog(@"ooh you clicked the annotation!");
}
}
还将 GMAP_ANNOTATION_SELECTED 设置为
NSString * const GMAP_MAP_SELECTED = @“地图选择”;
单击标记时得到的输出符合预期。但是当我单击地图区域时,我得到了相同的响应! (就像我点击了地图一样)。
我是否需要向 mkmap 添加某种侦听器来取消所有注释或其他内容?
I have a map marker to which I've added an event listener. when I click on the marker, I can get it to NSLog out a message... yet when I then click on the map, it does the same. I don't know if this is usual behaviour? In the end, I am trying to get a popup viewcontroller appearing - but that's been put on hold until this works.
So... I have sub-classed the annotationview which looks like this
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.image = [UIImage imageNamed:@"numberplate.png"];
self.frame = CGRectMake(0,0,133,40);
self.centerOffset = CGPointMake(76,0);
//self.selected = NO;
self.canShowCallout = NO;
}
return self;
}
Then in my main view controller, I have
- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {
VehicleViewInfo *eventView = (VehicleViewInfo *)[lmapView
dequeueReusableAnnotationViewWithIdentifier:
@"eventview"];
if(eventView == nil) {
eventView = [[[VehicleViewInfo alloc] initWithAnnotation:annotation
reuseIdentifier:@"eventview"]
autorelease];
}
[eventView addObserver:self
forKeyPath:@"selected"
options:NSKeyValueObservingOptionNew
context:GMAP_ANNOTATION_SELECTED];
eventView.annotation = annotation;
return eventView;
}
So then in my event listener, I have
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{
NSString *action = (NSString*)context;
NSLog(@"action received: %@", context);
if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){
NSLog(@"ooh you clicked the annotation!");
}
}
I also set the GMAP_ANNOTATION_SELECTED as
NSString * const GMAP_MAP_SELECTED =
@"mapselected";
The output I get when I click the marker is as expected. But when I click the map area, I get the same response! (as if I'd clicked on the map).
Do I need to add some sort of listener to the mkmap to cancel all annotations or something??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次“选定”属性发生更改时,您都会收到通知。因此,当您的视图被取消选择时,您将收到此通知。因此,如果您刚刚选择了此注释,然后点击其他位置来取消选择它,您将收到此通知两次。
顺便说一句,代码中可能存在微妙的错误:如果您最终使该注释视图出队,则可能会多次注册观察者。您需要确保在视图排队时删除观察者,或者在创建视图时简单地注册观察者一次(在“if(eventView == nil)”内)
You'll get a notification each time the property "selected" is changed. So you will get this notification when your view is deselected. So, if you have just selected this annotation and then tapped somewhere else to deselect it, you will have received this notification twice.
BTW, subtle possible bug in your code: if you end up dequeueing this annotation view, you may be registering the observer more than once. You need to make sure you remove the observer when the view is enqueued OR simply register the observer once when the view is created (within the 'if (eventView == nil)'