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
}
}
感謝您的版本rmaddy。仍然無法使這項工作:( –
[This](http://stackoverflow.com/questions/17132266/free-hand-drawing-in-google-maps-ios/40240153#40240153)可能會幫助 –