Jack Richards Jack Richards - 3 years ago 100
Swift Question

Using CLLocationManager() in viewDidLoad() prevents MKMapView from being dragged or zoomed

The map view refuses to be dragged or zoomed in/out with the following code.

Theres a number of things I've tried. I've allowed all the necessary properties in the attributes inspector and even tried adding the

isZoomEnabled
and other properties but with no luck.

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet var latitudeLabel: UILabel!
@IBOutlet var longitudeLabel: UILabel!
@IBOutlet var courseLabel: UILabel!
@IBOutlet var speedLabel: UILabel!
@IBOutlet var altitudeLabel: UILabel!
@IBOutlet var addressLabel: UILabel!
@IBOutlet var mapView: MKMapView!

var manager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations[0]

self.latitudeLabel.text = String(location.coordinate.latitude)
self.longitudeLabel.text = String(location.coordinate.longitude)
self.courseLabel.text = String(location.course)
self.speedLabel.text = String(location.speed)
self.altitudeLabel.text = String(location.altitude)

let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let latDelta: CLLocationDegrees = 0.10
let longDelta: CLLocationDegrees = 0.10
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinates, span: span)

self.mapView.setRegion(region, animated: true)

CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print(error!)
}

else {
if let placemark = placemarks?[0] {
var address = ""

if placemark.subThoroughfare != nil {
address += placemark.subThoroughfare! + " "
}

if placemark.thoroughfare != nil {
address += placemark.thoroughfare! + "\n"
}

if placemark.subLocality != nil {
address += placemark.subLocality! + "\n"
}

if placemark.subAdministrativeArea != nil {
address += placemark.subAdministrativeArea! + "\n"
}

if placemark.postalCode != nil {
address += placemark.postalCode! + "\n"
}

if placemark.country != nil {
address += placemark.country! + " "
}

self.addressLabel.text = address

}
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}


Where should this code go or what could be edited to fix this?

Answer Source

The map view's region is being set every time the location updates, preventing you from interacting with the map in a meaningful way.

One way to prevent this is to set a Bool indicating that the region has already been set and to not set it again.

var regionHasBeenSet = false

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    // ...

    if !regionHasBeenSet {
        let latitude = location.coordinate.latitude
        let longitude = location.coordinate.longitude
        let latDelta: CLLocationDegrees = 0.10
        let longDelta: CLLocationDegrees = 0.10
        let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
        let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let region = MKCoordinateRegion(center: coordinates, span: span)

        self.mapView.setRegion(region, animated: true)

        regionHasBeenSet = true
    }

    CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
        // ...
    }
}

And if you want the map's region to be centered on the location again, just change regionHasBeenSet to false whenever and on the next location update the region will be reset.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download