-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapViewController.swift
91 lines (77 loc) · 3.49 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
//
// MapViewController.swift
// JRNL
//
// Created by iOS17Programming on 07/10/2023.
//
import UIKit
import CoreLocation
import MapKit
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// MARK: - Properties
@IBOutlet var mapView: MKMapView!
let locationManager = CLLocationManager()
var selectedJournalEntry: JournalEntry?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
self.navigationItem.title = "Loading..."
mapView.delegate = self
}
override func viewIsAppearing(_ animated: Bool) {
super.viewIsAppearing(animated)
locationManager.requestLocation()
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let myCurrentLocation = locations.first {
let lat = myCurrentLocation.coordinate.latitude
let long = myCurrentLocation.coordinate.longitude
self.navigationItem.title = "Map"
mapView.region = setInitialRegion(lat: lat, long: long)
mapView.addAnnotations(SharedData.shared.getAllJournalEntries())
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to find user's location: \(error.localizedDescription)")
}
// MARK: - MKMapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "mapAnnotation"
if annotation is JournalEntry {
if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
annotationView.annotation = annotation
return annotationView
} else {
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
let calloutButton = UIButton(type: .detailDisclosure)
annotationView.rightCalloutAccessoryView = calloutButton
return annotationView
}
}
return nil
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let annotation = mapView.selectedAnnotations.first else { return }
selectedJournalEntry = annotation as? JournalEntry
self.performSegue(withIdentifier: "showMapDetail", sender: self)
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard segue.identifier == "showMapDetail" else {
fatalError("Unexpected segue identifier")
}
guard let entryDetailViewController = segue.destination as? JournalEntryDetailViewController else {
fatalError("Unexpected view controller")
}
entryDetailViewController.selectedJournalEntry = selectedJournalEntry
}
// MARK: - Private methods
func setInitialRegion(lat: CLLocationDegrees, long: CLLocationDegrees) -> MKCoordinateRegion {
MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: lat, longitude: long), span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
}
}