-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFilteringViewController.swift
82 lines (60 loc) · 2.82 KB
/
FilteringViewController.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
//
// FilteringViewController.swift
// Mapbox-iOS-Examples
//
// Created by Roman Blum on 21.09.16.
// Copyright © 2016 rmnblm. All rights reserved.
//
import UIKit
import Mapbox
class FilteringViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let dataSource = FilteringDataSource()
private var geoJSONSource: MGLShapeSource!
@IBOutlet var mapView: MGLMapView!
@IBOutlet var tableView: UITableView!
private func loadData() {
let geoJSONURL = Bundle.main.url(forResource: "siliconvalley", withExtension: "geojson")!
geoJSONSource = MGLShapeSource(identifier: "sv", url: geoJSONURL)
mapView.style?.addSource(geoJSONSource)
for layer in dataSource.layers {
let styleLayer = MGLSymbolStyleLayer(identifier: layer.title, source: geoJSONSource)
styleLayer.predicate = layer.predicate
//styleLayer.iconImage = MGLStyleConstantValue(rawValue: layer.name)
mapView.style?.addLayer(styleLayer)
layer.styleLayer = styleLayer
}
}
// MARK: MGLMapViewDelegate
public func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
loadData()
}
// MARK: UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.layers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? FilteringViewCell else {fatalError()}
let layer = dataSource.layers[(indexPath as NSIndexPath).row]
cell.titleLabel.text = layer.title
cell.stateSwitch.addTarget(self, action: #selector(filterStateChanged), for: UIControlEvents.valueChanged)
cell.stateSwitch.tag = indexPath.row
return cell
}
func filterStateChanged(sender: UISwitch) {
let layer = dataSource.layers[sender.tag]
layer.styleLayer.isVisible = sender.isOn
}
// MARK: UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! FilteringViewCell
cell.stateSwitch.setOn(!cell.stateSwitch.isOn, animated: true)
filterStateChanged(sender: cell.stateSwitch)
tableView.deselectRow(at: indexPath, animated: true)
}
}