Search code examples
swiftios9cllocationmanagercllocationcurrentlocation

locationManager not being recognized as a class variable


I am trying to implement the locationManager methods and the let locationManager = CLLocation() variable is not being recognized anywhere in the code. The errors showed up when I added the didUpdateLocations and didFailWithError methods. Im trying to get the current location from the user as the view loads. Without the methods mentioned above the app breaks on the centerMapOnLocation method.

Here is my code:

import Foundation
import UIKit
import Firebase
import FirebaseDatabase
import CoreLocation
import MapKit

class MainViewController: UIViewController, CLLocationManagerDelegate{

    var ref: FIRDatabaseReference!
    var refHandle: UInt!
    let locationManager: CLLocationManager = CLLocationManager()
    let regionRadius: CLLocationDistance = 1000
    var currentLocation: CLLocation!
    let geoCoder = CLGeocoder()
    var placemark: CLPlacemark?



    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var userEmailLabel: UILabel!
    @IBOutlet weak var pickUpAddress: UITextField!

    override func viewDidLoad() {
        ref = FIRDatabase.database().reference()


        ////
        locationManager.delegate = self
        locationManager.requestLocation()
        print(locationManager.requestLocation())
        ////



        ////
        refHandle = ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
            let dataDict = snapshot.value as! [String: AnyObject]

            print((dataDict))
        })
        let userID: String = FIRAuth.auth()!.currentUser!.uid
        ref.child("users").child(userID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        let userEmail = snapshot.value!["email"] as! String
        self.userEmailLabel.text = userEmail
        })
        ////



        ////
        centerMapOnLocation()
        super.viewDidLoad()
        ////
    }

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

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("error: \(error)")
    }
}

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status != CLAuthorizationStatus.Denied{
            locationManager.startUpdatingLocation()
    }
}



    func centerMapOnLocation() {
        currentLocation = CLLocation.init(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(locationManager.location!.coordinate, regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }


    func translateLocation() {
        geoCoder.reverseGeocodeLocation(currentLocation, completionHandler: { placemarks, error in
            if error == nil && placemarks!.count > 0 {
            self.placemark = placemarks![0] as CLPlacemark
                self.pickUpAddress.text = self.stringFromPlacemark(self.placemark!)
            }
        })
    }

    func stringFromPlacemark(placemark: CLPlacemark) -> String { // 1

        var line1 = ""

        if let s = placemark.subThoroughfare {
            line1 += s + " "
        }

        if let s = placemark.thoroughfare {
            line1 += s
        }
        var line2 = ""

        if let s = placemark.locality {
            line2 += s + " "
        }

        if let s = placemark.administrativeArea {
            line2 += s + " "
        }

        if let s = placemark.postalCode {
            line2 += s }
        return line1 + "\n" + line2
    }

    @IBAction func pickCurrentLocation(sender: AnyObject) {
       translateLocation()
    }

    @IBAction func signOutButton(sender: AnyObject) {
        try! FIRAuth.auth()!.signOut()
        if let storyboard = self.storyboard {
            let viewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController")
            self.presentViewController(viewController, animated: false, completion: nil)
        }
    }
}

I have not found an answer after a while now. Help is going to be really appreciated.

Thanks.


Solution

  • Remove the top-level closing brace somewhere on line 75 and everything will be fine, it builds for me, the thing is it sees these functions as global ones.