-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathYandexMapViewController.swift
161 lines (117 loc) · 4.78 KB
/
YandexMapViewController.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
//
// YandexMapViewController.swift
// Example-swift
//
// Created by petropavel on 24/01/2019.
// Copyright © 2019 Hulab. All rights reserved.
//
import YandexMapKit
import ClusterKit
import ExampleData
class YandexMapViewController: UIViewController, YMKMapViewDataSource,
YMKMapLoadedListener,
YMKMapCameraListener,
YMKMapObjectTapListener,
YMKMapObjectDragListener {
@IBOutlet weak var mapView: YMKMapView!
private var isMapLoaded = false
override func viewDidLoad() {
super.viewDidLoad()
mapView.dataSource = self
let map = mapView.mapWindow.map
map.setMapLoadedListenerWith(self)
map.addCameraListener(with: self)
let algorithm = CKNonHierarchicalDistanceBasedAlgorithm()
algorithm.cellSize = 200
mapView.clusterManager.algorithm = algorithm
mapView.clusterManager.marginFactor = 1
let paris = YMKPoint(latitude: 48.853, longitude: 2.35)
map.move(with: YMKCameraPosition(target: paris, zoom: 0, azimuth: 0, tilt: 0))
loadData()
}
func loadData() {
let operation = CKGeoPointOperation()
operation.setCompletionBlockWithSuccess({ (_, points) in
self.mapView.clusterManager.annotations = points
})
operation.start()
}
// MARK: YMKMapViewDataSource
func mapView(_ mapView: YMKMapView, placemarkFor cluster: CKCluster) -> YMKPlacemarkMapObject {
let point = YMKPoint(coordinate: cluster.coordinate)
let placemark: YMKPlacemarkMapObject
let mapObjects = mapView.mapWindow.map.mapObjects
if cluster.count > 1 {
placemark = mapObjects.addPlacemark(with: point, image: #imageLiteral(resourceName: "cluster"))
} else {
placemark = mapObjects.addPlacemark(with: point, image: #imageLiteral(resourceName: "marker"))
}
placemark.isDraggable = true
placemark.setDragListenerWith(self)
placemark.addTapListener(with: self)
return placemark
}
// MARK: YMKMapLoadedListener
func onMapLoaded(with statistics: YMKMapLoadStatistics) {
isMapLoaded = true
}
// MARK: - How To Update Clusters
// MARK: YMKMapCameraListener
func onCameraPositionChanged(with map: YMKMap,
cameraPosition: YMKCameraPosition,
cameraUpdateSource: YMKCameraUpdateSource,
finished: Bool) {
guard isMapLoaded, finished else {
return
}
mapView.clusterManager.updateClustersIfNeeded()
// workaround (https://github.com/yandex/mapkit-ios-demo/issues/23)
// we need to wait some time to get actual visibleRegion for clusterization
// DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
// self.mapView.clusterManager.updateClustersIfNeeded()
// }
}
// MARK: - How To Handle Selection/Deselection
// MARK: YMKMapObjectTapListener
func onMapObjectTap(with mapObject: YMKMapObject, point: YMKPoint) -> Bool {
guard let placemark = mapObject as? YMKPlacemarkMapObject,
let cluster = placemark.cluster else {
return false
}
let padding = UIEdgeInsets(top: 40, left: 20, bottom: 44, right: 20)
if cluster.count > 1 {
mapView.mapWindow.map.move(with: mapView.cameraPositionThatFits(cluster, edgePadding: padding),
animationType: YMKAnimation(type: .smooth, duration: 0.5),
cameraCallback: { completed in
if completed {
self.mapView.clusterManager.updateClustersIfNeeded()
}
})
return true
} else if let annotation = cluster.firstAnnotation {
mapView.clusterManager.selectAnnotation(annotation, animated: true)
return true
}
return false
}
// MARK: - How To Handle Drag and Drop
// MARK: YMKMapObjectDragListener
func onMapObjectDragStart(with mapObject: YMKMapObject) {
// nothing
}
func onMapObjectDrag(with mapObject: YMKMapObject, point: YMKPoint) {
// nothing
}
func onMapObjectDragEnd(with mapObject: YMKMapObject) {
guard let placemark = mapObject as? YMKPlacemarkMapObject,
let annotation = placemark.cluster?.firstAnnotation as? MKPointAnnotation else {
return
}
annotation.coordinate = placemark.geometry.coordinate
}
}
private extension YMKPoint {
convenience init(coordinate: CLLocationCoordinate2D) {
self.init(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
}