-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathMapViewController.swift
295 lines (239 loc) · 10.6 KB
/
MapViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// MapViewController.swift
// MapObjects
//
import UIKit
import YandexMapsMobile
class MapViewController: UIViewController {
// MARK: - Public methods
override func viewDidLoad() {
super.viewDidLoad()
mapView = YMKMapView(frame: view.frame)
view.addSubview(mapView)
map = mapView.mapWindow.map
mapView.mapWindow.addSizeChangedListener(with: mapSizeChangedListener)
updateMapFocus()
map.move(with: GeometryProvider.startPosition)
pinsCollection = map.mapObjects.add()
clusterizedCollection = pinsCollection.addClusterizedPlacemarkCollection(with: clusterListener)
polygonMapObject = pinsCollection.addPolygon(with: GeometryProvider.polygon)
polygonMapObject.strokeWidth = 1.5
polygonMapObject.strokeColor = Palette.olive
polygonMapObject.fillColor = Palette.oliveTransparent
polylineMapObject = pinsCollection.addPolyline(with: GeometryProvider.polyline)
polylineMapObject.strokeWidth = 5.0
polylineMapObject.setStrokeColorWith(.gray)
polylineMapObject.outlineWidth = 1.0
polylineMapObject.outlineColor = .black
polylineMapObject.addTapListener(with: mapObjectTapListener)
let circle = pinsCollection.addCircle(with: GeometryProvider.circleWithRandomRadius)
circle.strokeColor = Palette.red
circle.strokeWidth = 2.0
circle.fillColor = Palette.redTransparent
circle.addTapListener(with: mapObjectTapListener)
addClusterizablePlacemarks()
addPlacemarks()
setupSubviews()
}
// MARK: - Private methods
/// Sets the map to specified point, zoom, azimuth and tilt
private func move(_ map: YMKMap, to cameraPosition: YMKCameraPosition = GeometryProvider.startPosition) {
map.move(with: cameraPosition, animation: YMKAnimation(type: .smooth, duration: 1.0))
}
/// Sets the map to specified geometry
private func move(_ map: YMKMap, to geometry: YMKGeometry) {
let cameraPosition = map.cameraPosition(with: geometry)
map.move(with: cameraPosition, animation: YMKAnimation(type: .smooth, duration: 1.0))
}
private func addClusterizablePlacemarks() {
let iconStyle = YMKIconStyle()
iconStyle.anchor = NSValue(cgPoint: CGPoint(x: 0.5, y: 0.5))
iconStyle.scale = 0.6
GeometryProvider.clusterizedPoints.enumerated().forEach { pair in
let index = pair.offset
let point = pair.element
let type = PlacemarkType.random
let image = type.image
let placemark = clusterizedCollection.addPlacemark()
placemark.geometry = point
placemark.setIconWith(image, style: iconStyle)
placemark.isDraggable = true
placemark.setDragListenerWith(mapObjectDragListener)
placemark.userData = PlacemarkUserData(name: "Data_\(index)", type: type)
placemark.addTapListener(with: mapObjectTapListener)
}
clusterizedCollection.clusterPlacemarks(
withClusterRadius: GeometryProvider.clusterRadius,
minZoom: GeometryProvider.clusterMinZoom
)
}
private func addPlacemarks() {
let placemark = pinsCollection.addPlacemark()
placemark.geometry = GeometryProvider.compositeIconPoint
placemark.addTapListener(with: singlePlacemarkTapListener)
placemark.setTextWithText(
"Special place",
style: {
let textStyle = YMKTextStyle()
textStyle.size = 10.0
textStyle.placement = .right
textStyle.offset = 5.0
return textStyle
}()
)
let compositeIcon = placemark.useCompositeIcon()
compositeIcon.setIconWithName(
"pin",
image: UIImage(named: "icon_dollar")!,
style: {
let iconStyle = YMKIconStyle()
iconStyle.anchor = NSValue(cgPoint: CGPoint(x: 0.5, y: 1.0))
iconStyle.scale = 0.9
return iconStyle
}()
)
compositeIcon.setIconWithName(
"point",
image: UIImage(named: "icon_circle")!,
style: {
let iconStyle = YMKIconStyle()
iconStyle.anchor = NSValue(cgPoint: CGPoint(x: 0.5, y: 0.5))
iconStyle.scale = 0.5
iconStyle.flat = true
return iconStyle
}()
)
if let animatedImageProvider = YRTAnimatedImageProviderFactory.fromFile(
Bundle.main.path(forResource: "animation", ofType: "png")
) as? YRTAnimatedImageProvider {
let animatedPlacemark = pinsCollection.addPlacemark()
animatedPlacemark.geometry = GeometryProvider.animatedImagePoint
let iconStyle = YMKIconStyle()
iconStyle.scale = 0.6
animatedPlacemark.useAnimation().setIconWithImage(animatedImageProvider, style: iconStyle)
animatedPlacemark.useAnimation().play()
}
}
/// Updates map focus
private func updateMapFocus() {
let scale = Float(UIScreen.main.scale)
mapView.mapWindow.focusRect = YMKScreenRect(
topLeft: YMKScreenPoint(x: Float(Layout.horizontalMargin) * scale, y: Float(Layout.verticalMargin) * scale),
bottomRight: YMKScreenPoint(
x: Float(view.frame.width - Layout.horizontalMargin) * scale,
y: Float(view.frame.height - Layout.verticalMargin) * scale
)
)
}
// MARK: - Private properties
private var mapView: YMKMapView!
private lazy var map: YMKMap = mapView.mapWindow.map
private var pinsCollection: YMKMapObjectCollection!
private var clusterizedCollection: YMKClusterizedPlacemarkCollection!
private var polygonMapObject: YMKPolygonMapObject!
private var polylineMapObject: YMKPolylineMapObject!
private lazy var mapSizeChangedListener = MapSizeChangedListener { [weak self] in
self?.updateMapFocus()
}
private lazy var mapObjectTapListener = MapObjectTapListener(controller: self)
private lazy var singlePlacemarkTapListener = PlacemarkMapObjectTapListener(
controller: self,
alertTitle: "Tapped the placemark with composite icon"
)
private lazy var animatedPlacemarkTapListener = PlacemarkMapObjectTapListener(
controller: self,
alertTitle: "Tapped the animated placemark"
)
private lazy var mapObjectDragListener = MapObjectDragListener(
controller: self,
clusterizedCollection: clusterizedCollection
)
private lazy var clusterListener = ClusterListener(controller: self)
private lazy var geometryVisitorViewModel = GeometryVisitorViewModel()
private lazy var geometryVisibilityVisitor = GeometryVisibilityVisitor(viewModel: geometryVisitorViewModel)
// MARK: - Layout
private enum Layout {
static let horizontalMargin: CGFloat = 40.0
static let verticalMargin: CGFloat = 60.0
static let buttonSize: CGFloat = 50.0
static let buttonCornerRadius: CGFloat = 8.0
}
private func setupSubviews() {
view.addSubview(toolbarView)
toolbarView.axis = .horizontal
toolbarView.alignment = .fill
toolbarView.distribution = .equalSpacing
toolbarView.translatesAutoresizingMaskIntoConstraints = false
[
toolbarView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: Layout.horizontalMargin),
toolbarView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -Layout.horizontalMargin),
toolbarView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -Layout.verticalMargin)
]
.forEach { $0.isActive = true }
[changeCollectionVisibilityButton, focusPolylineButton, focusPolygonButton, changeGeometryVisibilityButton]
.forEach {
toolbarView.addArrangedSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.layer.cornerRadius = Layout.buttonCornerRadius
[
$0.heightAnchor.constraint(equalToConstant: Layout.buttonSize),
$0.widthAnchor.constraint(equalToConstant: Layout.buttonSize)
]
.forEach { $0.isActive = true }
$0.backgroundColor = Palette.background
}
changeCollectionVisibilityButton.setImage(UIImage(systemName: "mappin.slash.circle.fill"), for: .normal)
focusPolygonButton.setImage(UIImage(systemName: "hexagon"), for: .normal)
focusPolylineButton.setImage(UIImage(systemName: "square.split.diagonal.2x2"), for: .normal)
changeGeometryVisibilityButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
changeCollectionVisibilityButton.addTarget(
self,
action: #selector(changeCollectionVisibilityButtonTapHandler),
for: .touchUpInside
)
focusPolygonButton.addTarget(
self,
action: #selector(focusPolygonButtonTapHandler),
for: .touchUpInside
)
focusPolylineButton.addTarget(
self,
action: #selector(focusPolylineButtonTapHandler),
for: .touchUpInside
)
changeGeometryVisibilityButton.addTarget(
self,
action: #selector(changeGeometryVisibilityButtonTapHandler),
for: .touchUpInside
)
}
@objc
private func changeCollectionVisibilityButtonTapHandler() {
AlertPresenter.present(from: self, with: "\(pinsCollection.isVisible ? "Hiding" : "Showing") all objects")
pinsCollection.isVisible.toggle()
}
@objc
private func focusPolygonButtonTapHandler() {
let geometry = YMKGeometry(polygon: polygonMapObject.geometry)
move(map, to: geometry)
}
@objc
private func focusPolylineButtonTapHandler() {
let geometry = YMKGeometry(polyline: polylineMapObject.geometry)
move(map, to: geometry)
}
@objc
private func changeGeometryVisibilityButtonTapHandler() {
AlertPresenter.present(
from: self,
with: "Turning geometry \(geometryVisitorViewModel.isGeometryShownOnMap ? "off" : "on")"
)
geometryVisitorViewModel.isGeometryShownOnMap.toggle()
pinsCollection.traverse(with: geometryVisibilityVisitor)
}
private let toolbarView = UIStackView()
private let changeCollectionVisibilityButton = UIButton()
private let focusPolygonButton = UIButton()
private let focusPolylineButton = UIButton()
private let changeGeometryVisibilityButton = UIButton()
}