3

我正在尝试在我的地图上创建一个多边形以显示边界。但是当我运行我的应用程序时,什么都没有出现。我可以看到地图但看不到多边形我的错误是什么?谢谢。

import UIKit    
import MapKit    
import CoreLocation

class MapScreen : UIViewController {

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        mapView.delegate = self as! MKMapViewDelegate

        // center the location on the screen
        var location = CLLocationCoordinate2DMake(41.308792, -72.928641)
        var span = MKCoordinateSpanMake(0.01, 0.01)
        var region = MKCoordinateRegionMake(location, span)
        mapView.setRegion(region, animated: true)

        //calling the method
        addBoundry()


    }

    func addBoundry(){ //creation of a polygon

        var points = [CLLocationCoordinate2DMake(41.311674, -72.925506),
                      CLLocationCoordinate2DMake(41.307308, -72.928694),
                      CLLocationCoordinate2DMake(41.307108, -72.928324),
                      CLLocationCoordinate2DMake(41.307892, -72.930285),
                      CLLocationCoordinate2DMake(41.307892, -72.931223),
                      CLLocationCoordinate2DMake(41.307227, -72.932494),
                      CLLocationCoordinate2DMake(41.308452, -72.931663),
                      CLLocationCoordinate2DMake(41.308730, -72.932773),
                      CLLocationCoordinate2DMake(41.308496, -72.931614),
                      CLLocationCoordinate2DMake(41.308496, -72.931614),
                      CLLocationCoordinate2DMake(41.311288, -72.931630),
                      CLLocationCoordinate2DMake(41.311659, -72.930945),
                      CLLocationCoordinate2DMake(41.312893, -72.932153),
                      CLLocationCoordinate2DMake(41.313433, -72.930542),
                      CLLocationCoordinate2DMake(41.313324, -72.929963),
                      CLLocationCoordinate2DMake(41.312758, -72.929027),
                      CLLocationCoordinate2DMake(41.312373, -72.927167),
                      CLLocationCoordinate2DMake(41.311674, -72.925506)]

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

        mapView.add(polygon)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolygon {
            let polygonView = MKPolygonRenderer(overlay: overlay)
            polygonView.fillColor = UIColor(red: 0, green: 0.847, blue: 1, alpha: 0.25)

            return polygonView
        }
        return nil    
    }
}
4

1 回答 1

4

您的签名mapView(_:rendererFor:)不正确。在 Swift 3 中,它将是:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    ...
}

如果您正式声明您遵守协议之类的协议MKMapViewDelegate,它通常会警告您这些事情。例如,最佳实践是在类扩展中而不是在类定义本身中这样做:

extension MapScreen: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        ...
    }

}

顺便说一句,如果你这样做,你不仅会收到有关协议一致性的警告,而且在设置时也不需要强制转换delegate

mapView.delegate = self
于 2017-07-11T15:39:03.697 回答