如何在 Xcode swift 3 中添加另一个覆盖时删除一个覆盖

how to delete one overlay when another is added in Xcode swift 3

我正在尝试在我的应用程序中构建一个功能,允许用户在某个位置放置图钉并显示该位置周围的半径。我只想一次显示一个图钉和一个半径。

我已经弄清楚如何放下图钉并添加半径,以及如何在放下新图钉时移除旧图钉,但无法弄清楚如何删除圆形叠加层以便只显示一个,周围最近掉落的引脚。

下面是我的地图视图控制器的代码。帮助非常感谢!

import UIKit
import CoreLocation
import MapKit

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    //connect map 
    @IBOutlet weak var mapInterface: MKMapView!

    let manager = CLLocationManager()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations[0] //all locations will be stored in CLLocation array, we request 0th element (the newest data which equals the most recent location)
        print(location)
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        print(location)

        mapInterface.setRegion(region, animated: true)
        self.mapInterface.showsUserLocation = true 
    }

    //add pin drop 
    @IBAction func pinDrop(_ sender: UILongPressGestureRecognizer) {
        //Pin drop annotation - get attributes of where to drop the pin
        let location = sender.location(in: self.mapInterface)
        let locCoord = self.mapInterface.convert(location, toCoordinateFrom: self.mapInterface)
        let annotation = MKPointAnnotation()

        //set pin characteristics
        annotation.coordinate = locCoord
        annotation.title = "Virtual location"
        annotation.subtitle = "Dropped Pin"

        //delete one pin once another is dropped
        self.mapInterface.removeAnnotations(mapInterface.annotations) 

        //add pin annotation to map view
        self.mapInterface.addAnnotation(annotation)

        //print locCoord to console to check it worked
        print(locCoord)

        //create circle attributes
        let cent = locCoord
        let rad: Double = 500 //adjust radius to make circle bigger.
        let circle = MKCircle(center: cent, radius: rad)

        //print circle to console to check it worked
        print(circle)
    }

    //add circle overlay 
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay.isKind(of: MKCircle.self){
            let circleRenderer = MKCircleRenderer(overlay: overlay)
            circleRenderer.fillColor = UIColor.blue.withAlphaComponent(0.05)
            circleRenderer.strokeColor = UIColor.blue
            circleRenderer.lineWidth = 0.5
            return circleRenderer
        }
        self.mapInterface.removeOverlays(overlay as! [MKOverlay])
        return MKOverlayRenderer(overlay: overlay)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation() 
        manager.stopUpdatingLocation() 
    }
}

pinDrop方法中添加以下代码。

self.mapInterface.removeOverlays(self.mapInterface.overlays)

mapView.removeOverlays(mapView.overlays) 会成功

但是,如果我是你,我宁愿更新注释位置而不是重新创建一个新的并删除旧的

要做到这一点,您将乘坐那条线

let annotation = MKPointAnnotation()

并把它放在viewDidLoad方法上面

而不是:

//delete one pin once another is 
  self.mapInterface.removeAnnotations(mapInterface.annotations) 
//add pin annotation to map view
  self.mapInterface.addAnnotation(annotation)

它将是:

if self.mapInterface.annotations.count == 0 {
    self.mapInterface.addAnnotation(annotation)
}