2016-11-07 132 views
1

我一直在做一個項目,斯威夫特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 
    } 
} 
+0

感謝您的版本rmaddy。仍然無法使這項工作:( –

+0

[This](http://stackoverflow.com/questions/17132266/free-hand-drawing-in-google-maps-ios/40240153#40240153)可能會幫助 –

回答

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