-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapViewController.swift
148 lines (113 loc) · 5.04 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
import UIKit
import MapKit
import CoreData
class MapViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
var locations = [Location]()
var managedObjectContext: NSManagedObjectContext! {
didSet {
NSNotificationCenter.defaultCenter().addObserverForName(
NSManagedObjectContextObjectsDidChangeNotification,
object: managedObjectContext,
queue: NSOperationQueue.mainQueue()) { _ in
if self.isViewLoaded() {
self.updateLocations()
}
}
}
}
@IBAction func showUser() {
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
@IBAction func showLocations() {
let region = regionForAnnotations(locations)
mapView.setRegion(region, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
updateLocations()
if !locations.isEmpty {
showLocations()
}
}
func updateLocations() {
mapView.removeAnnotations(locations)
let entity = NSEntityDescription.entityForName("Location", inManagedObjectContext: managedObjectContext)
let fetchRequest = NSFetchRequest()
fetchRequest.entity = entity
locations = try! managedObjectContext.executeFetchRequest(fetchRequest) as! [Location]
mapView.addAnnotations(locations)
}
func regionForAnnotations(annotations: [MKAnnotation]) -> MKCoordinateRegion {
var region: MKCoordinateRegion
switch annotations.count {
case 0:
region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)
case 1:
let annotation = annotations[annotations.count - 1]
region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000)
default:
var topLeftCoord = CLLocationCoordinate2D(latitude: -90, longitude: 180)
var bottomRightCoord = CLLocationCoordinate2D(latitude: 90, longitude: 180)
for annotation in annotations {
topLeftCoord.latitude = max(topLeftCoord.latitude, annotation.coordinate.latitude)
topLeftCoord.longitude = min(topLeftCoord.longitude, annotation.coordinate.longitude)
bottomRightCoord.latitude = min(bottomRightCoord.latitude, annotation.coordinate.latitude)
bottomRightCoord.longitude = max(bottomRightCoord.longitude, annotation.coordinate.longitude)
}
let centerLatitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) / 2
let centerLongitude = topLeftCoord.longitude - (topLeftCoord.longitude - bottomRightCoord.longitude) / 2
let center = CLLocationCoordinate2D(latitude: centerLatitude, longitude: centerLongitude)
let extraSpace = 1.1
let span = MKCoordinateSpan(latitudeDelta: abs(topLeftCoord.latitude - bottomRightCoord.latitude) * extraSpace, longitudeDelta: abs(topLeftCoord.longitude - bottomRightCoord.longitude) * extraSpace)
region = MKCoordinateRegion(center: center, span: span)
}
return mapView.regionThatFits(region)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditLocation" {
let navigationController = segue.destinationViewController as! UINavigationController
let controller = navigationController.topViewController as! LocationDetailsViewController
controller.managedObjectContext = managedObjectContext
let button = sender as! UIButton
let location = locations[button.tag]
controller.locationToEdit = location
}
}
}
extension MapViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is Location else {
return nil
}
let identifier = "Location"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as! MKPinAnnotationView!
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.enabled = true
annotationView.canShowCallout = true
annotationView.animatesDrop = false
annotationView.pinTintColor = UIColor(red: 0.32, green: 0.82, blue: 0.4, alpha: 1)
annotationView.tintColor = UIColor(white: 0.0, alpha: 0.5)
let rightButton = UIButton(type: .DetailDisclosure)
rightButton.addTarget(self, action: #selector(MapViewController.showLocationDetails(_:)), forControlEvents: .TouchUpInside)
annotationView.rightCalloutAccessoryView = rightButton
} else {
annotationView.annotation = annotation
}
let button = annotationView.rightCalloutAccessoryView as! UIButton
if let index = locations.indexOf(annotation as! Location) {
button.tag = index
}
return annotationView
}
func showLocationDetails(sender: UIButton) {
performSegueWithIdentifier("EditLocation", sender: sender)
}
}
extension MapViewController: UINavigationBarDelegate {
func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
return .TopAttached
}
}