This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
MapViewController.swift
executable file
·458 lines (398 loc) · 16 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import MapKit
import Firebase
import GoogleMaps
import MaterialComponents
import Platform
class MapViewController: UIViewController {
let appBar = MDCAppBar()
fileprivate enum Constants {
/// Location of the venue. The large venue marker is displayed at this location.
static let venueCoordinates = CLLocationCoordinate2D(latitude: 37.426360,
longitude: -122.079552)
static let degreesLongitude = 0.005900
static let degreesLatitude = 0.005600
static let startingLongitude = -122.082526
static let startingLatitude = 37.422600
static let overlaySouthWest = CLLocationCoordinate2D(latitude: startingLatitude, longitude: startingLongitude)
static let overlayNorthEast = CLLocationCoordinate2D(latitude: startingLatitude + degreesLatitude,
longitude: startingLongitude + degreesLongitude)
static let venueCameraZoomDefault: Float = 17.7
static let venueCameraZoom: Float = 17.7
static let defaultBearing: Double = 0
static let overlayWidth: CGFloat = 950
static let overlayHeight: CGFloat = 1170
static let labelMarkerExtraPaddingWidth: CGFloat = 27
static let labelMarkerExtraPaddingHeight: CGFloat = 0
static let markerWidth: CGFloat = 27
static let markerHeight: CGFloat = 60
static let markerFont = MDCTypography.fontLoader().mediumFont(ofSize: 12)!
/// Original position of the camera that shows the venue.
static let cameraPosition = GMSCameraPosition(target: Constants.venueCoordinates,
zoom: Constants.venueCameraZoomDefault,
bearing: Constants.defaultBearing,
viewingAngle: 0.0)
static let APIKey = Configuration.sharedInstance.googleMapsApiKey
static let headerBackgroundColor = UIColor.white
static let titleColor = UIColor(hex: "#747474")
static let titleHeight: CGFloat = 24.0
static let titleFont = "Product Sans"
static let title = NSLocalizedString("Map", comment: "Title for map page")
static let filterButtonTitle =
NSLocalizedString("Filter", comment: "Button title that opens a list of filters")
static let enableFilterFeature = false
static let placeIcon = "ic_place"
}
// MARK: - Properties
private static var initializedAPIKey = false
fileprivate lazy var mapView: GMSMapView = self.setupMapView()
private lazy var overlay: GMSOverlay = self.setupOverlay()
private lazy var card: MapCardView = self.setupMapCardView()
fileprivate let viewModel: MapViewModel
private let locationManager: CLLocationManager
fileprivate var googleMarkers = [GMSMarker]()
fileprivate var activeMarkers = [GMSMarker]()
fileprivate var isCardVisible = false {
didSet {
card.isHidden = !isCardVisible
}
}
init(viewModel: MapViewModel) {
self.viewModel = viewModel
locationManager = CLLocationManager()
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
viewModel.update {
self.refreshUI()
}
}
func refreshUI() {
// Remove any existing markers.
for marker in googleMarkers {
marker.map = nil
marker.userData = nil
}
googleMarkers.removeAll()
// Create new markers from updated view model.
let newMarkers: [GMSMarker] = viewModel.mapItems.map { mapItem in
let position =
CLLocationCoordinate2D(latitude: mapItem.latitude, longitude: mapItem.longitude)
let googleMarker = GMSMarker(position: position)
googleMarker.title = mapItem.title
googleMarker.appearAnimation = GMSMarkerAnimation.pop
var width = CGFloat(Constants.markerWidth)
var height = CGFloat(Constants.markerHeight)
if let title = googleMarker.title {
width = title.boundingRect(with: CGSize(width: 1000, height: Constants.markerHeight),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: [NSAttributedStringKey.font: Constants.markerFont],
context: nil).size.width
width += Constants.labelMarkerExtraPaddingWidth
height += Constants.labelMarkerExtraPaddingHeight
}
let iconView = MapMarkerIconView(frame: CGRect(x: 0, y: 0, width: width, height: height),
mapItemType: mapItem.type)
iconView.title = googleMarker.title
googleMarker.iconView = iconView
if mapItem.type == .label {
googleMarker.groundAnchor = CGPoint(x: 0.5, y: 0)
}
googleMarker.tracksViewChanges = false
googleMarker.map = mapView
googleMarker.userData = mapItem
googleMarker.zIndex = mapItem.type == .label ? 2 : 1
return googleMarker
}
googleMarkers.append(contentsOf: newMarkers)
updateMarkerVisibility()
}
func select(roomId: String?) {
guard let roomId = roomId else { return }
let matchingMarker = googleMarkers.first { marker in
if let mapItem = mapItem(marker: marker), mapItem.id == roomId {
return true
}
return false
}
guard let marker = matchingMarker else { return }
selectActiveMarker(marker: marker)
}
func setupFilterButton() -> UIBarButtonItem {
let button = UIBarButtonItem.init(title: Constants.filterButtonTitle,
style: .plain,
target: self,
action: #selector(filterAction))
button.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: Constants.titleColor],
for: .normal)
return button
}
func setupCenterButton() -> UIBarButtonItem {
let button = UIBarButtonItem.init(image: UIImage(named: Constants.placeIcon)?.withRenderingMode(.alwaysTemplate),
style: .plain,
target: self,
action: #selector(centerAction))
button.tintColor = Constants.titleColor
button.accessibilityLabel = NSLocalizedString("Center conference on map", comment: "Accessibility label for center button.")
return button
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Application.sharedInstance.analytics.logEvent(AnalyticsEventSelectContent, parameters: [
AnalyticsParameterContentType: AnalyticsParameters.screen,
AnalyticsParameterItemID: AnalyticsParameters.map
])
// Don't request until after the screen is displayed to the user to give more context.
locationManager.requestWhenInUseAuthorization()
mapView.isMyLocationEnabled = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
mapView.isMyLocationEnabled = false
super.viewWillDisappear(animated)
}
private func setupMapView() -> GMSMapView {
// API key should not be initialized more than once.
if !MapViewController.initializedAPIKey {
let apiKey = Constants.APIKey
if !GMSServices.provideAPIKey(apiKey) {
NSLog("Google maps API key invalid.")
}
MapViewController.initializedAPIKey = true
}
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: Constants.cameraPosition)
do {
if let styleURL = Bundle.main.url(forResource: "maps_style", withExtension: "json") {
mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
} else {
NSLog("Unable to find style.json")
}
} catch {
NSLog("One or more of the map styles failed to load. \(error)")
}
// Turn on accessibility, it is turned off by default.
mapView.accessibilityElementsHidden = false
mapView.settings.myLocationButton = true
mapView.settings.compassButton = true
overlay.map = mapView
return mapView
}
private func setupOverlay() -> GMSOverlay {
let mapImage = UIImage(named: "map.png")
let overlayBounds = GMSCoordinateBounds(coordinate: Constants.overlaySouthWest, coordinate: Constants.overlayNorthEast)
let overlay = GMSGroundOverlay(bounds: overlayBounds, icon: mapImage)
overlay.bearing = 0
return overlay
}
private func setupMapCardView() -> MapCardView {
return MapCardView()
}
private func setupViews() {
self.title = Constants.title
self.addChildViewController(appBar.headerViewController)
appBar.headerViewController.headerView.backgroundColor = Constants.headerBackgroundColor
appBar.navigationBar.tintColor = Constants.titleColor
let font = UIFont(name: Constants.titleFont, size: Constants.titleHeight)
var attributes: [NSAttributedStringKey: Any] =
[ NSAttributedStringKey.foregroundColor: Constants.titleColor ]
if let font = font {
attributes[NSAttributedStringKey.font] = font
}
appBar.navigationBar.titleTextAttributes = attributes
edgesForExtendedLayout = []
mapView.delegate = self
view.addSubview(mapView)
mapView.translatesAutoresizingMaskIntoConstraints = false
card.delegate = self
view.addSubview(card)
card.translatesAutoresizingMaskIntoConstraints = false
isCardVisible = activeMarkers.count >= 1
appBar.addSubviewsToParent()
if (Constants.enableFilterFeature) {
self.navigationItem.rightBarButtonItems = [setupFilterButton(), setupCenterButton()]
} else {
self.navigationItem.rightBarButtonItems = [setupCenterButton()]
}
let views: [String: UIView] = ["mapView": mapView, "headerView": appBar.headerViewController.headerView, "card": card]
var constraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[mapView]|",
options: [],
metrics: nil,
views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[headerView][mapView]|",
options: [],
metrics: nil,
views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:[card]-8-|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-6-[card]-6-|", options: [], metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
}
func deselectActiveMarkers() {
if activeMarkers.count > 0 {
for marker in activeMarkers {
marker.tracksViewChanges = true
if let customIconView = markerIconView(marker: marker) {
customIconView.selected = false
}
marker.tracksViewChanges = false
}
}
activeMarkers.removeAll(keepingCapacity: true)
isCardVisible = false
}
func selectActiveMarker(marker: GMSMarker) {
guard !activeMarkers.contains(marker) else {
return
}
deselectActiveMarkers()
activeMarkers.append(marker)
marker.tracksViewChanges = true
let update = GMSCameraUpdate.setTarget(marker.position)
mapView.animate(with: update)
if let customIconView = markerIconView(marker: marker) {
customIconView.selected = true
}
marker.tracksViewChanges = false
if let mapItem = mapItem(marker: marker), mapItem.title != "" {
card.title = mapItem.title
card.details = mapItem.description
isCardVisible = true
logSelectedMapItem(withTitle: mapItem.title)
} else {
isCardVisible = false
}
}
func logSelectedMapItem(withTitle title: String) {
Application.sharedInstance.analytics.logEvent(AnalyticsEventSelectContent, parameters: [
AnalyticsParameterItemID: AnalyticsParameters.itemID(forPinTitle: title),
AnalyticsParameterContentType: AnalyticsParameters.uiEvent,
AnalyticsParameters.uiAction: AnalyticsParameters.mapPinSelect
])
}
func mapItem(marker: GMSMarker) -> MapItemViewModel? {
if let mapItem = marker.userData as? MapItemViewModel {
return mapItem
}
return nil
}
func markerIconView(marker: GMSMarker) -> MapMarkerIconView? {
return marker.iconView as? MapMarkerIconView
}
func pairedMarker(markerNeedingPair: GMSMarker) -> GMSMarker? {
guard let mapItemNeedingPair = mapItem(marker: markerNeedingPair) else { return nil }
guard let tagNeedingPair = mapItemNeedingPair.tag else { return nil }
for marker in googleMarkers {
if marker == markerNeedingPair {
continue
}
if let mapItem = mapItem(marker: marker), let tag = mapItem.tag, tag == tagNeedingPair {
return marker
}
}
return nil
}
func updateMarkerVisibility() {
// Hide or show markers based on zoom level.
let position = mapView.camera
if position.zoom < Constants.venueCameraZoom {
for marker in googleMarkers {
marker.map = nil
}
} else if !viewModel.anyItemsSelected {
// Check if no items are selected, meaning we should show everything.
for marker in googleMarkers {
marker.map = mapView
}
} else {
// Check each individual selection state and add any matching labels.
var visibleMarkers: [GMSMarker] = []
for marker in googleMarkers {
if let mapItem = mapItem(marker: marker), mapItem.selected {
visibleMarkers.append(marker)
if let pairedMaker = pairedMarker(markerNeedingPair: marker) {
visibleMarkers.append(pairedMaker)
}
}
}
for marker in googleMarkers {
if visibleMarkers.contains(marker) {
marker.map = mapView
} else {
marker.map = nil
}
}
}
}
}
// MARK: - Actions
extension MapViewController {
@objc func filterAction() {
let filterViewController = MapFilterViewController(viewModel: viewModel, delegate: self)
self.present(filterViewController, animated: true, completion: nil)
}
@objc func centerAction() {
mapView.animate(to: Constants.cameraPosition)
}
}
// MARK: - MapFilterViewControllerDelegate
extension MapViewController: MapFilterViewControllerDelegate {
func viewControllerDidFinish() {
self.presentedViewController?.dismiss(animated: true, completion: {
})
// Update map based on filtering.
updateMarkerVisibility()
}
}
// MARK: - MapCardViewDelegate
extension MapViewController: MapCardViewDelegate {
func viewDidTapDismiss() {
isCardVisible = false
}
}
// MARK: - GMSMapViewDelegate
extension MapViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
var markerToSelect = marker
// If the marker is a label, try to select the matching marker.
if let mapIcon = markerIconView(marker: marker), mapIcon.mapItemType == .label {
if let pairedMarker = pairedMarker(markerNeedingPair: marker) {
markerToSelect = pairedMarker
} else {
return true
}
}
if activeMarkers.contains(markerToSelect) {
deselectActiveMarkers()
} else {
selectActiveMarker(marker: markerToSelect)
}
return true
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
deselectActiveMarkers()
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
updateMarkerVisibility()
}
}