创建叠加层/多边形Mapkit Swift 5

问题描述

我尝试制作覆盖图,但地图上没有任何显示

我要绘制4条线,使其形状像正方形,将显示在地图上(我添加了4条CLLocationCoord)。

我在做什么错了?

我应该添加一些代码吗?

我试图添加mapView.delegate = self,但是我不知道为什么它不起作用。

导入UIKit

导入MapKit

导入CoreLocation

class ViewController: UIViewController {
    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    let regionInMeters: Double = 1000
    
    override func viewDidLoad() {
        super.viewDidLoad()
       checkLocationServices()
    

        //calling the method
                addBoundry()


            }

            func addBoundry(){ //creation of a polygon

                var points = [CLLocationCoordinate2DMake(52.284428,20.989394),CLLocationCoordinate2DMake(52.224534,21.044326),CLLocationCoordinate2DMake(52.209182,20.948024),CLLocationCoordinate2DMake(52.247143,20.918842),]

                let polygon = MKpolygon(coordinates: &points,count: points.count)

                mapView.addOverlay(polygon)
            }
            func mapView(mapView: MKMapView,rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
                if overlay is MKpolygon {
                    let polygonView = mkpolygonrenderer(overlay: overlay)
                    polygonView.strokeColor = .magenta

                    return polygonView
                }
                return MKOverlayRenderer()
                
                
    }
    
    func setupLocationManager(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
    }
    
    func centerViewOnUserLocation () {
        if let location = locationManager.location?.coordinate {
            let region = MKCoordinateRegion.init(center: location,latitudinalMeters:  regionInMeters,longitudinalMeters: regionInMeters)
            mapView.setRegion(region,animated: true)
        }
    }

    func checkLocationServices() {
        if   CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuthorization()
        } else {
            
            // Show alert letting the user kNow they have to turn this on.
            
        }
    }

    func checkLocationAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            mapView.showsUserLocation = true
            centerViewOnUserLocation()
            locationManager.startUpdatingLocation()
            break
        case .denied:
            // Show alert instructing them how to turn on perm
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            // Show an alert letting them kNow what's up
            break
        case .authorizedAlways:
            break
    
        }
    }
    
}

extension ViewController: CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
       guard let location = locations.last else {return}
        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion.init(center: center,latitudinalMeters: regionInMeters,longitudinalMeters: regionInMeters)
        mapView.setRegion(region,animated: true)
        
    }
    
    func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }

}

解决方法

mapView.delegate = self

在您的类实现MKMapViewDelegate时起作用。

类似

class ViewController: UIViewController,MKMapViewDelegate {