代码之家  ›  专栏  ›  技术社区  ›  joneswah

如何关闭mkmapview中mkannotation的标注

  •  16
  • joneswah  · 技术社区  · 17 年前

    我有一个mkmapview,它有许多注释。选择管脚将显示标注,然后按附件将弹出一个新的视图控制器到堆栈上。但是,当我从新的VC中按回时,标注仍处于打开状态。如何关闭它?

    我试过了

    if([[myMapView selectedAnnotations] count] > 0)
    {
        //deselect that annotation
        [myMapView deselectAnnotation:[[myMapView selectedAnnotations] objectAtIndex:0] animated:NO];
    }
    

    但这不起作用。selectedAnnotations在数组中没有一个条目,因此它会进入此语句,但调用未关闭。

    我是否需要向我的mkannotation实现或mkpinannotationView添加一些内容?

    6 回复  |  直到 10 年前
        1
  •  34
  •   fklappan    12 年前

    selectedAnnotations中的对象是mkAnnotation的实例

    NSArray *selectedAnnotations = mapView.selectedAnnotations;
    for(id annotation in selectedAnnotations) {
        [mapView deselectAnnotation:annotation animated:NO];
    }
    
        2
  •  14
  •   leviathan    15 年前

    如果你想坚持使用地图工具包文档。

    for (NSObject<MKAnnotation> *annotation in [mapView selectedAnnotations]) {
        [mapView deselectAnnotation:(id <MKAnnotation>)annotation animated:NO];
    }
    
        3
  •  2
  •   Chris Neefus    10 年前
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView,
                 calloutAccessoryControlTapped control: UIControl)
    {
        let pin = view.annotation
        mapView.deselectAnnotation(pin, animated: false)
        performSegueWithIdentifier("Next VC Segue", sender: nil)
    }
    

    在进入新的视图控制器之前取消选择注释。这样你回来的时候它就不见了。

        4
  •  1
  •   joneswah    17 年前

    代替一个好的解决方案,下面的hacky方法将在视图中显示:动画

        for( MyMapAnnotation *aMKAnn in  [myMapView annotations])
        {
            //dodgy select then deselect each annotation
            [myMapView selectAnnotation:aMKAnn animated:NO];
            [myMapView deselectAnnotation:aMKAnn animated:NO];
        }
    

    selectedAnnotations数组没有1个值,但取消选择该值仍不能关闭调用?所以我只需遍历所有注释并选择和取消选择。我没有太多的注释,所以可能不会对性能造成太大影响?

    如果有人有更好的想法,我会欣赏优雅的解决方案吗?

        5
  •  0
  •   Daniel    17 年前

    当你重新扣上别针时,标注应该消失…

        6
  •  0
  •   Aswathy Bose    13 年前
    - (void)deselectAllAnnotations
    {
    
        NSArray *selectedAnnotations = [self.mapViewObj.mapView selectedAnnotations];
        for (int i = 0; i < [selectedAnnotations count]; i++) {
            [self.mapViewObj.mapView deselectAnnotation:[selectedAnnotations objectAtIndex:i] animated:NO];
        }
    
    }
    

    这可以帮助你解决你的问题。