KellysOnTop23 KellysOnTop23 - 4 months ago 14
iOS Question

CLLocation Manager using default location and not updating with actual location

So I have no idea what happened. The code I had was working perfectly fine and then i worked around in story board for a bit and suddenly I started getting the error that my CLLocation was always nil when being unwrapped. I did everything i could think of or read online.


  • added to .plist

  • copied code in different spots

  • moved the didUpdateLocations into viewDidLoad



If I update the scheme to have a default location then It just uses the default location the whole time and never updates with my current location, same when i try it on an actual device but if I leave it to none then the delegate method for error
didFailWithError
is called saying that the value returned was nil. I am losing it over here. Please Help.

enter image description here

enter image description here
enter image description here

override func viewDidLoad() {
super.viewDidLoad()

self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()

}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("In the location, seems we got some shit --> didFailWithError: \(error.description)")

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

let locValue : CLLocationCoordinate2D = locationManager.location!.coordinate
let geoFire = GeoFire(firebaseRef: self.geofireRef.child("Posts"))

let center = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)

let circleQuery = geoFire.queryAtLocation(center, withRadius: 10)

circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
})

self.locationManager.stopUpdatingLocation()

}

Answer

Running the following bit of code:

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    var locationManager = CLLocationManager()
    var mapView = MKMapView()
    var screenSize = UIScreen.mainScreen().bounds

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.distanceFilter = kCLDistanceFilterNone
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()

        mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
        mapView.showsUserLocation = true
        mapView.delegate = self
        view.addSubview(mapView)

        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("In the location, seems we got some error \(error.description)")
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue : CLLocationCoordinate2D = locationManager.location!.coordinate
        let center = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
        let region = MKCoordinateRegionMakeWithDistance(locValue, 2000, 2000)
        mapView.setRegion(region, animated: true)
        mapView.centerCoordinate = locValue
        print(center)
    }
}

Setting the requirements in the Info.plist of course and enabling the location simulator in debug menu section:

location simulator

Gives me the following output:

<+37.33756603,-122.04120235> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/7/16, 11:27:25 PM Central European Summer Time
<+37.33756603,-122.04120235> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/7/16, 11:27:26 PM Central European Summer Time