迅速删除地图叠加

我正试图从地图上删除覆盖。

func removeMapOverlay() { var removeOverlays : [AnyObject]! = self.mapView.overlays // Above line throws runtime exception self.mapView.removeOverlays(removeOverlays) } 

self.mapView.overlaysAnyObject数组的types。 var overlays: [AnyObject]! { get } var overlays: [AnyObject]! { get }

所以最初我写了

 var removeOverlays = self.mapView.overlays 

它会在运行时在此行抛出EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)exception。

所以我没有为[AnyObject]types转换,我不知道它是正确的,但它仍然给我在运行时相同的exception。

编辑:

我为Objective C代码做的是:

 - (void) removeMapOverlay { [self.mapView removeOverlays:[self.mapView overlays]]; NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.mapView annotations]]; if ([tempArray containsObject:[MKUserLocation class]]) { [tempArray removeObject:[MKUserLocation class]]; } NSArray *annotationArray = [NSArray arrayWithArray:tempArray]; tempArray = nil; [self.mapView removeAnnotations:annotationArray]; } 

我试图在Swift中创build类似的方法。 但它引发了一个像我上面解释的exception。

我刚刚做了这个:

 let overlays = mapView.overlays mapView.removeOverlays(overlays) 

它似乎工作。 为什么你将你的覆盖层定义为一个静态variables?

编辑:

这就是我们所做的能够使用全局包含函数来search一个快速数组,并检查我们正在寻找的元素是否存在于数组中。 在这种情况下,我们正在数组中searchCLLocationCoordinate2D。

 public func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool { return lhs.longitude == rhs.longitude && lhs.latitude == rhs.latitude } public func == (lhs: MKMapPoint, rhs: MKMapPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } extension CLLocationCoordinate2D: Equatable{ } //This is needed to be Equatable to use the global contains function extension MKMapPoint: Equatable { } 

这对我工作:

 self.mapView.overlays.forEach { if !($0 is MKUserLocation) { self.mapView.removeOverlay($0) } } 

对于谷歌地图的iOS映射(Swift 3和最新的iOS SDK), 文档 ,你将设置覆盖为零像这样

 overlay.map = nil