2016-09-11 8 views
1

私はlocationManagerメソッドを実装しようとしていますが、let locationManager = CLLocation()変数はコード内のどこでも認識されません。 didUpdateLocationsメソッドとdidFailWithErrorメソッドを追加したときにエラーが表示されました。ビューが読み込まれるときにユーザーから現在の場所を取得しようとしています。上記のメソッドがなければ、アプリケーションはcenterMapOnLocationメソッドでブレークします。locationManagerがクラス変数として認識されない

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) 
     } 
    } 
} 

私は今しばらくして答えを見つけることができます:

は、ここに私のコードです。ヘルプは本当に感謝されるつもりです。

ありがとうございました。

+0

元の質問とは関係ありませんが、最初からではなく、実装の最後に 'super.viewDidLoad()'を呼び出す理由がありますか? – bneely

答えて

3

75行目のどこかにある最上位の閉じ括弧を削除すると、すべてがうまくなります。それは私のために構築されますが、それはグローバルなものとしてこれらの機能を見ていることです。

関連する問題