iPad MapKit - 重新选择一个图钉不起作用

发布于 2024-11-04 10:43:39 字数 2066 浏览 1 评论 0原文

我遇到以下问题:

我的地图上有很多图钉。触摸其中一个后,将打开一个自定义弹出窗口并显示信息。这很完美。

我的问题是,如果您选择同一个图钉两次,弹出窗口不会打开,您必须触摸另一个图钉或单击地图中的某个位置。

我的代码如下:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKAnnotationView* annotationView = nil;
    CPPointOfInterest *myAnnotation = (CPPointOfInterest*) annotation;

    if ([myAnnotation isKindOfClass:[MKUserLocation class]] || myAnnotation.poi_id <= 0) {
        return nil;

    } else {
        NSString* identifier = @"Pin";
        MKPinAnnotationView* annView = (MKPinAnnotationView*)[mv_map dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(nil == annView) {
            annView = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
        }

        [annView addObserver:self
                  forKeyPath:@"selected"
                     options:NSKeyValueObservingOptionNew
                 context:@"GMAP_ANNOTATION_SELECTED"];

        if(self.nearest_poi == myAnnotation) {
            annView.pinColor = MKPinAnnotationColorGreen;

        } else {
            annView.pinColor = MKPinAnnotationColorRed;

        }

        annView.animatesDrop = YES;

        annotationView = annView;

        [annotationView setEnabled:YES];
        [annotationView setCanShowCallout:NO];

        return annotationView;

    }
}

观察者:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;
    if([action isEqualToString:@"GMAP_ANNOTATION_SELECTED"]){
        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            // show popup
            } 
    }
}

我还尝试了 didSelectAnnotationView 方法,这也仅适用于第一次单击事件。

我已经搜索了几个小时,但没有找到...:/

有人知道我的问题的解决方案吗,请给我提示...

谢谢和问候, 马修

I have the following problem:

I have a number of pins on my map. After touching one, a custom popup opens and displays information. This works perfect.

My problem is, that if you select the same pin twice, the popups does not open, you have to touch another one or click some where in the map.

My code is the following:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKAnnotationView* annotationView = nil;
    CPPointOfInterest *myAnnotation = (CPPointOfInterest*) annotation;

    if ([myAnnotation isKindOfClass:[MKUserLocation class]] || myAnnotation.poi_id <= 0) {
        return nil;

    } else {
        NSString* identifier = @"Pin";
        MKPinAnnotationView* annView = (MKPinAnnotationView*)[mv_map dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(nil == annView) {
            annView = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
        }

        [annView addObserver:self
                  forKeyPath:@"selected"
                     options:NSKeyValueObservingOptionNew
                 context:@"GMAP_ANNOTATION_SELECTED"];

        if(self.nearest_poi == myAnnotation) {
            annView.pinColor = MKPinAnnotationColorGreen;

        } else {
            annView.pinColor = MKPinAnnotationColorRed;

        }

        annView.animatesDrop = YES;

        annotationView = annView;

        [annotationView setEnabled:YES];
        [annotationView setCanShowCallout:NO];

        return annotationView;

    }
}

Observer:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;
    if([action isEqualToString:@"GMAP_ANNOTATION_SELECTED"]){
        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            // show popup
            } 
    }
}

I have also tried the didSelectAnnotationView methode, this also just works for the first click event.

I have searched for several hours, but nothing to find... :/

Does anybody knows a solution for my problem, please give me a hint...

Thanks and greetings,
Mathew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-11-11 10:43:39

您必须通过mapView取消选择注释,然后才能再次选择它。请尝试以下操作:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

[mapView deselectAnnotation:view.annotation animated:NO];

}

You have to deselect the annotation through the mapView before you can select it again. Try the folowing:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

[mapView deselectAnnotation:view.annotation animated:NO];

}
戴着白色围巾的女孩 2024-11-11 10:43:39

我正在使用弹出窗口来显示信息:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    if (popover)
    {
        [popover release];
        popover = nil;
    }

    LocatorSearchDealer *dealer = [[mapView selectedAnnotations] objectAtIndex:0];

    //without this line I cannot immediately reselect the same annotation.
    [mapView deselectAnnotation:dealer animated:NO];
}

I'm using a popover to display the info:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    if (popover)
    {
        [popover release];
        popover = nil;
    }

    LocatorSearchDealer *dealer = [[mapView selectedAnnotations] objectAtIndex:0];

    //without this line I cannot immediately reselect the same annotation.
    [mapView deselectAnnotation:dealer animated:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文