【问题标题】:Creating Overlay / Polygon Mapkit Swift 5创建覆盖/多边形 Mapkit Swift 5
【发布时间】:2020-12-31 16:44:59
【问题描述】:

我尝试制作叠加层,但地图上没有显示任何内容。

我想制作一个 4 条线,形成一个像正方形的形状,这将显示在地图上(我添加了 4 个 CLLocationCoord)。

我做错了什么?

我应该添加一些代码吗?

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

导入 UIKit

导入 MapKit

导入核心位置

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()
    }

}

【问题讨论】:

    标签: mapkit overlay polygon swift5 xcode11


    【解决方案1】:
    mapView.delegate = self
    

    当您的类实现 MKMapViewDelegate 时有效。

    类似

    class ViewController: UIViewController, MKMapViewDelegate {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      相关资源
      最近更新 更多