-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.swift
78 lines (59 loc) · 3.6 KB
/
ViewController.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
//
// ViewController.swift
// CapitalCities
//
// Created by Julian Moorhouse on 12/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
title = "Capital Cities"
let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics", wikiUrl: "https://en.wikipedia.org/wiki/London")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago", wikiUrl: "https://en.wikipedia.org/wiki/oslo")
let paris = Capital(title: "Paris", coordinate: CLLocationCoordinate2D(latitude: 48.8567, longitude: 2.3508), info: "Often called the City of Light", wikiUrl: "https://en.wikipedia.org/wiki/Paris")
let rome = Capital(title: "Rome", coordinate: CLLocationCoordinate2D(latitude: 41.9, longitude: 12.5), info: "Has a whole country inside it.", wikiUrl: "https://en.wikipedia.org/wiki/Rome")
let washington = Capital(title: "Washington DC", coordinate: CLLocationCoordinate2D(latitude: 38.895111, longitude: -77.036667), info: "Named after George himself.", wikiUrl: "https://en.wikipedia.org/wiki/Washington,_D.C.")
mapView.addAnnotations([london, oslo, paris, rome, washington])
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(changeMapStyle))
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is Capital else { return nil }
let identifier = "Capital"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
let btn = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = btn
} else {
annotationView?.annotation = annotation
}
annotationView?.pinTintColor = .cyan
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let capital = view.annotation as? Capital else { return }
mapView.deselectAnnotation(view.annotation, animated: true)
if let vc = storyboard?.instantiateViewController(withIdentifier: "sb_DetailViewController") as? DetailViewController {
vc.capital = capital
navigationController?.pushViewController(vc, animated: true)
}
}
@objc func changeMapStyle() {
let ac = UIAlertController(title: "Map Style", message: nil, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "Hybrid", style: .default, handler: { (action) in
self.mapView.mapType = .hybrid
}))
ac.addAction(UIAlertAction(title: "Satellite", style: .default, handler: { (action) in
self.mapView.mapType = .satellite
}))
ac.addAction(UIAlertAction(title: "Standard", style: .default, handler: { (action) in
self.mapView.mapType = .standard
}))
present(ac, animated: true)
}
}