@@ -23,10 +23,11 @@ struct PointsInfoToPass {
23
23
}
24
24
}
25
25
26
- class ViewController : UIViewController {
26
+ class ViewController : UIViewController , MKMapViewDelegate {
27
27
28
28
private var mapView : MKMapView !
29
29
private let regionRadius : CLLocationDistance = 1000
30
+ private var pointsSelected : [ MKPointAnnotation ] = [ ]
30
31
31
32
private var choosePointButton : UIButton = {
32
33
let btn = UIButton ( )
@@ -55,8 +56,9 @@ class ViewController: UIViewController {
55
56
56
57
private func setUp( ) {
57
58
self . view. backgroundColor = . white
58
-
59
+
59
60
self . mapView = MKMapView ( frame: CGRect ( x: 0 , y: 50 , width: self . view. frame. width, height: 300 ) )
61
+ self . mapView. delegate = self
60
62
let stack = UIStackView ( )
61
63
let stackHeight : CGFloat = 500
62
64
stack. translatesAutoresizingMaskIntoConstraints = false
@@ -79,10 +81,21 @@ class ViewController: UIViewController {
79
81
mapView. setRegion ( coordinateRegion, animated: true )
80
82
}
81
83
84
+ func mapView( _ mapView: MKMapView , rendererFor overlay: MKOverlay ) -> MKOverlayRenderer {
85
+
86
+ if overlay . isKind ( of: MKPolygon . self) {
87
+ let renderer = MKPolygonRenderer ( overlay: overlay)
88
+ renderer. fillColor = UIColor ( white: 0.5 , alpha: 0.5 )
89
+ return renderer
90
+ }
91
+ return MKOverlayRenderer ( overlay: overlay)
92
+ }
93
+
82
94
@objc func choosePoint( _ sender: UIButton ) {
83
95
let mapVC = ChoosePointController ( info: PointsInfoToPass ( del: self , points: [ ] ) )
84
96
let navVC = UINavigationController ( rootViewController: mapVC)
85
97
navVC. modalPresentationStyle = . fullScreen
98
+ self . mapView. removeAnnotations ( self . pointsSelected)
86
99
self . present ( navVC, animated: true )
87
100
}
88
101
@@ -91,10 +104,16 @@ class ViewController: UIViewController {
91
104
extension ViewController : SelectPointsDel {
92
105
func didSelectPoints( points: [ MKPointAnnotation ] ) {
93
106
guard !points. isEmpty else { return }
107
+ self . pointsSelected = points
108
+ var coordinates = [ CLLocationCoordinate2D] ( )
94
109
for point in points {
95
110
self . mapView. addAnnotation ( point)
111
+ coordinates. append ( point. coordinate)
96
112
}
97
113
self . mapView. setRegion ( MKCoordinateRegion ( center: points [ 0 ] . coordinate, latitudinalMeters: self . regionRadius, longitudinalMeters: regionRadius) , animated: true )
114
+ let polygon = MKPolygon ( coordinates: & coordinates, count: coordinates. count)
115
+ self . mapView. addOverlay ( polygon)
116
+
98
117
}
99
118
}
100
119
0 commit comments