-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapViewController.swift
233 lines (182 loc) · 8.68 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// MapViewController.swift
// Bio
//
// Created by Ann McDonough on 10/13/20.
// Copyright © 2020 Patrick McDonough. All rights reserved.
//
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
var spot: Spot!
let regionDistance: CLLocationDistance = 750
var userDataVM: UserDataVM?
// MARK: - Outlets
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!
var currentLocation: CLLocation!
// MARK: - Search
fileprivate var searchController: UISearchController!
fileprivate var localSearchRequest: MKLocalSearch.Request!
fileprivate var localSearch: MKLocalSearch!
fileprivate var localSearchResponse: MKLocalSearch.Response!
// MARK: - Map variables
fileprivate var annotation: MKAnnotation!
// fileprivate var locationManager: CLLocationManager!
fileprivate var isCurrentLocation: Bool = false
// MARK: - Activity Indicator
fileprivate var activityIndicator: UIActivityIndicatorView!
// MARK: - UIViewController's methods
override func viewDidLoad() {
super.viewDidLoad()
searchBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
mapView.frame = CGRect(x: 0, y: searchBar.frame.maxY, width: self.view.frame.width, height: self.view.frame.height - searchBar.frame.height)
let currentLocationButton = UIBarButtonItem(title: "Current Location", style: UIBarButtonItem.Style.plain, target: self, action: #selector(MapViewController.currentLocationButtonAction(_:)))
self.navigationItem.leftBarButtonItem = currentLocationButton
let searchButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.search, target: self, action: #selector(MapViewController.searchButtonAction(_:)))
self.navigationItem.rightBarButtonItem = searchButton
mapView.delegate = self
mapView.mapType = .hybrid
activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
activityIndicator.hidesWhenStopped = true
self.view.addSubview(activityIndicator)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activityIndicator.center = self.view.center
}
// MARK: - Actions
func updateUserInterface() {
updateMap()
}
func updateMap() {
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotation(spot)
mapView.setCenter(spot.coordinate, animated: true)
}
@objc func currentLocationButtonAction(_ sender: UIBarButtonItem) {
if (CLLocationManager.locationServicesEnabled()) {
if locationManager == nil {
locationManager = CLLocationManager()
}
locationManager?.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
isCurrentLocation = true
}
}
// MARK: - Search
@objc func searchButtonAction(_ button: UIBarButtonItem) {
if searchController == nil {
searchController = UISearchController(searchResultsController: nil)
}
searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.delegate = self
present(searchController, animated: true, completion: nil)
}
// MARK: - UISearchBarDelegate
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
dismiss(animated: true, completion: nil)
if self.mapView.annotations.count != 0 {
annotation = self.mapView.annotations[0]
self.mapView.removeAnnotation(annotation)
}
localSearchRequest = MKLocalSearch.Request()
localSearchRequest.naturalLanguageQuery = searchBar.text
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { [weak self] (localSearchResponse, error) -> Void in
if localSearchResponse == nil {
let alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
alert.show()
return
}
let pointAnnotation = MKPointAnnotation()
pointAnnotation.title = searchBar.text
pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)
let pinAnnotationView = MKPinAnnotationView(annotation: pointAnnotation, reuseIdentifier: nil)
self!.mapView.centerCoordinate = pointAnnotation.coordinate
self!.mapView.addAnnotation(pinAnnotationView.annotation!)
}
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if !isCurrentLocation {
return
}
isCurrentLocation = false
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
if self.mapView.annotations.count != 0 {
annotation = self.mapView.annotations[0]
self.mapView.removeAnnotation(annotation)
}
let pointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = location!.coordinate
pointAnnotation.title = ""
mapView.addAnnotation(pointAnnotation)
}
}
extension MapViewController: CLLocationManagerDelegate {
func getLocation(){
locationManager = CLLocationManager()
locationManager.delegate = self
}
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
}
func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .authorizedAlways, .authorizedWhenInUse:
locationManager.requestLocation()
case .denied:
print("I'm sorry - can't show location. User has not authorized it.")
case .restricted:
print("Access deined. Likely parental controls restrict location services in this app.")
showAlert(title: "Access Denied.", message: "Likely parental controls restrict location services in this app.")
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
handleLocationAuthorizationStatus(status: status)
}
// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// guard spot.name == "" else {
// return
//// }
// let geoCoder = CLGeocoder()
// var name = ""
// var address = ""
// currentLocation = locations.last
// spot.coordinate = currentLocation.coordinate
// geoCoder.reverseGeocodeLocation(currentLocation, completionHandler: {placemarks, error in
// if placemarks != nil {
// let placemark = placemarks?.last
// name = placemark?.name ?? "name unknown"
// //need to import conetacts to use this code
//
// if let postalAddress = placemark?.postalAddress {
// address = CNPostalAddressFormatter.string(from: postalAddress, style: .mailingAddress)
// }
// } else {
// print("error retreiving place. error code: \(error!.localizedDescription)")
// }
// self.spot.name = name
// self.spot.address = address
// self.updateUserInterface()
// })
// }
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to get user location")
}
}