用颜色填充地图中的区域

时间:2016-11-07 16:42:22

标签: swift mkmapview swift3

我一直在Swift 3中做一个项目。

我通过触摸地图来选择我的点数,之后我用一条线将它们链接起来。

我一直试图用红色填充这些点之间的区域,但没有任何成功。我该怎样才能完成这个?

我在mapview中设置了填充颜色,但它无效。

到目前为止我有这个代码:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet var map: MKMapView!

    var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()   //all touched points

    override func viewDidLoad() {
        super.viewDidLoad()

        map.delegate = self

        let latitude: CLLocationDegrees = 38.925560
        let longitude: CLLocationDegrees = -9.229723
        let lanDelta: CLLocationDegrees = 0.05
        let lonDelta: CLLocationDegrees = 0.05
        let span = MKCoordinateSpan(latitudeDelta: lanDelta, longitudeDelta: lonDelta)
        let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let region = MKCoordinateRegion(center: coordinates, span: span)

        map.setRegion(region, animated: true)

        let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)))

        uilpgr.minimumPressDuration = 0.5

        map.addGestureRecognizer(uilpgr)
    }

    func longpress(gestureRecognizer: UIGestureRecognizer) {
        guard gestureRecognizer.state == .began else { return }

        let touchPoint = gestureRecognizer.location(in: self.map)
        let coordinate = map.convert(touchPoint, toCoordinateFrom: self.map)
        let annotation = MKPointAnnotation()

        annotation.coordinate = coordinate
        annotation.title = "My Place"
        map.addAnnotation(annotation)

        // print("longpress activated")
        points.append(annotation.coordinate)

        let polyline = MKPolyline(coordinates: points, count: points.count)
        map.add(polyline)
    }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.blue
        polylineRenderer.fillColor = UIColor.red
        polylineRenderer.lineWidth = 5
        return polylineRenderer
    }
}

1 个答案:

答案 0 :(得分:1)

MKPolyLine将渲染一条线。如果你想要一个填充多边形,你需要MKPolyon。如果你想要一个轮廓和一个填充,请同时使用它们。

    fileprivate var polygon: MKPolygon? = nil

    func addPolygon() {
          let polygon = MKPolygon(coordinates: &polygonCoordinates, count: polygonCoordinates.count)
          self.polygon = polygon
          mapView.add(polygon)
    }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if let _ = overlay as? MKPolyline {
            let renderer = MKPolylineRenderer(polyline: polyLine!)
            renderer.strokeColor = UIColor.blue
            renderer.lineWidth = 2
            return renderer
        } else {
            let renderer = MKPolygonRenderer(polygon: polygon)
            renderer.fillColor = UIColor.blue.withAlphaComponent(0.25)
            return renderer
        }
    }