locationManager 未被识别为类变量
Posted
技术标签:
【中文标题】locationManager 未被识别为类变量【英文标题】:locationManager not being recognized as a class variable 【发布时间】:2017-01-19 05:21:53 【问题描述】:我正在尝试实现 locationManager 方法,而 let locationManager = CLLocation() 变量在代码中的任何地方都没有被识别。当我添加 didUpdateLocations 和 didFailWithError 方法时,错误出现了。我试图在视图加载时从用户那里获取当前位置。如果没有上面提到的方法,应用程序会在 centerMapOnLocation 方法上中断。
这是我的代码:
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)
一段时间后我还没有找到答案。帮助将不胜感激。
谢谢。
【问题讨论】:
与您的原始问题无关,但您是否有充分的理由在实施结束时而不是在开始时调用super.viewDidLoad()
?
【参考方案1】:
删除第 75 行某处的***右大括号,一切都会好起来的,它为我构建,问题是将这些函数视为全局函数。
【讨论】:
以上是关于locationManager 未被识别为类变量的主要内容,如果未能解决你的问题,请参考以下文章
ipython 未被识别为内部或外部命令 (pyspark)
Apollo Server,datasource-sql 子类未被识别为有效的 DataSource 类型