Skip to content

Commit

Permalink
Feature: Go to current location if no geofences have been set
Browse files Browse the repository at this point in the history
  • Loading branch information
below committed Oct 5, 2022
1 parent 7d231a1 commit b802cb6
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions GeofenceTester/RegionsListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RegionsListViewController: UIViewController {
var locationManager: CLLocationManager = CLLocationManager()
var storage = PersistantStorage<EventRecord>()
internal var logger = os.Logger()
var firstUse = true

@IBOutlet var mapView: MKMapView!
@IBOutlet var addButton: UIBarButtonItem!
Expand All @@ -38,6 +39,10 @@ class RegionsListViewController: UIViewController {
addButton.accessibilityLabel = NSLocalizedString("Settings", comment: "Accessibilty label for settings button")

locationManager.delegate = self

self.mapView.setCenter(
self.mapView.userLocation.coordinate,
animated: false)
registerMapAnnotationViews()

switch self.locationManager.authorizationStatus {
Expand All @@ -54,6 +59,13 @@ class RegionsListViewController: UIViewController {
preferredStyle: .alert)
self.present(alert, animated: true)
}

NotificationCenter.default.addObserver(
forName: RegionsListViewController.UpdateNotificationName,
object: nil,
queue: .main) { _ in
self.updateMap()
}
}

internal func startLocationServices() {
Expand All @@ -67,13 +79,6 @@ class RegionsListViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
updateMap()

NotificationCenter.default.addObserver(
forName: RegionsListViewController.UpdateNotificationName,
object: nil,
queue: .main) { _ in
self.updateMap()
}
}

let defaultReuseIdentifier = "defaultAnnotationView"
Expand Down Expand Up @@ -211,6 +216,18 @@ class RegionsListViewController: UIViewController {
}

extension RegionsListViewController: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
if self.locationManager.monitoredRegions.count == 0 {
if firstUse {
mapView.setCenter(
userLocation.coordinate,
animated: false)
}
}
firstUse = false
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !annotation.isKind(of: MKUserLocation.self) else {
// Make a fast exit if the annotation is the `MKUserLocation`, as it's not an annotation view we wish to customize.
Expand Down

0 comments on commit b802cb6

Please sign in to comment.