Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion iOSDevUK/Controllers/Maps/AllLocationsMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class AllLocationsMapViewController: UIViewController, UITableViewDelegate, UITa

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// In case this is the first time showing a map screen
// request permission to use the user's location
CLLocationManager().requestWhenInUseAuthorization()
}

// MARK: - Navigation
Expand Down Expand Up @@ -77,11 +81,12 @@ class AllLocationsMapViewController: UIViewController, UITableViewDelegate, UITa
func showMapLocations(forIndexPath indexPath: IndexPath) {
mapView.removeAnnotations(mapView.annotations)

guard let locations = locationTypes?[indexPath.row].locations else {
guard let locationTypes = locationTypes else {
print("Unable to access list of locations")
return
}

let locations = locationTypes[indexPath.row].locations
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above two line changes relate to when there's no data downloaded and locationTypes is nil, which would then still try to subscript the nil array, which caused a crash

showMapAnnotations(locations: locations)

let region = regionForLocations(locations: locations)
Expand All @@ -90,6 +95,10 @@ class AllLocationsMapViewController: UIViewController, UITableViewDelegate, UITa
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Avoid overriding the user's location annotation view, use the default
guard !(annotation is MKUserLocation) else {
return nil
}

let identifier = "iosdevukLocation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
Expand Down
12 changes: 12 additions & 0 deletions iOSDevUK/Controllers/Maps/MapLocationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@ class MapLocationViewController: UIViewController, MKMapViewDelegate, SFSafariVi
// Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// In case this is the first time showing a map screen
// request permission to use the user's location
CLLocationManager().requestWhenInUseAuthorization()
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Avoid overriding the user's location annotation view, use the default
guard !(annotation is MKUserLocation) else {
return nil
}

let identifier = "iosdevukLocation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
Expand Down
2 changes: 2 additions & 0 deletions iOSDevUK/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>The app will show your location on maps it presents</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
Loading