Создание оверлея/многоугольника 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()
    }

}

person Hubert    schedule 13.09.2020    source источник


Ответы (1)


mapView.delegate = self

работает, когда ваш класс реализует MKMapViewDelegate.

что-то типа

class ViewController: UIViewController, MKMapViewDelegate {
person Gerd Castan    schedule 02.10.2020