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

标签 ios swift mapkit overlay

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

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

下面是我的 map View Controller 的代码。非常感谢帮助!

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)

关于ios - 在 Xcode swift 3 中添加另一个覆盖层时如何删除一个覆盖层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49418293/

相关文章:

ios - 如何在 iOS 中将 map 区域限制在一个国家?

ios - MKMapView setRegion 不工作

php - ipad 网站上的电话号码消失了

iOS 异常回溯 - 应用程序条目未符号化

xcode - 基于Xcode版本的条件编译

ios - 如何在 Swift 3 中显示 MKCircle

c# - Xamarin - 设置跨平台 Hello World App

ios - Localizable.strings 的字符编码,由 genstrings 生成

swift - Rx swift : Nested Queries and ReplaySubject

swift - 如何在 Swift 中使用 Discarding TaskGroup 获取任务结果