-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapyViewController.swift
107 lines (85 loc) · 3.57 KB
/
MapyViewController.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
//
// MapyViewController.swift
// Example
//
// Created by Josef Dolezal on 04/09/2018.
// Copyright © 2018 Josef Dolezal. All rights reserved.
//
import UIKit
import MapKit
import MapyKit
import MapyAPI
class MapyViewController: UIViewController {
// MARK: Properties
private static let mapTypes = ExtendedMapType.allCases
@IBOutlet weak var mapyView: MapyView!
@IBOutlet weak var tableView: UITableView!
private let service = MapyAPIService()
private var currentMapType = ExtendedMapType.standard
// MARK: Controller lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Setup table view
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
tableView.register(MapTypeCell.self, forCellReuseIdentifier: MapTypeCell.identifier)
view.addSubview(tableView)
NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
tableView.heightAnchor.constraint(equalToConstant: 250)
])
// Setup mapy view
let mapyView = MapyView(frame: view.frame)
mapyView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapyView.delegate = self
mapyView.setExtendedMapType(currentMapType)
mapyView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "Annotation")
self.mapyView = mapyView
view.insertSubview(mapyView, belowSubview: tableView)
// Add annotation
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 50.0713667, longitude: 14.4010147)
mapyView.addAnnotation(annotation)
}
}
// MARK: - UITableViewDataSource
extension MapyViewController: UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MapyViewController.mapTypes.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MapTypeCell.identifier, for: indexPath)
let mapType = MapyViewController.mapTypes[indexPath.row]
cell.textLabel?.text = mapType.title
cell.detailTextLabel?.text = mapType.isMultilayerType
? "Multi-layer map type"
: "Single-layer map type"
cell.accessoryType = mapType == currentMapType
? .checkmark
: .none
return cell
}
}
// MARK: - UITableViewDelegate
extension MapyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let mapType = MapyViewController.mapTypes[indexPath.row]
tableView.deselectRow(at: indexPath, animated: true)
// Set new map type to the map view
self.currentMapType = mapType
mapyView.setExtendedMapType(mapType)
// Reload the table view data
tableView.reloadData()
}
}
// MARK: - MKMapViewDelegate
extension MapyViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
mapyView.dequeueReusableAnnotationView(withIdentifier: "Annotation", for: annotation)
}
}