swift - 在 map View 上使用轨迹位置时出现变量错误

标签 swift geolocation core-location

我正在尝试在 map View 上显示和跟踪位置。我收到错误:

Use of local Variable 'locationManager' before declaration

该错误出现在 locationManager 的所有行上:

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

我在 ViewDidLoad 之前声明了 var locationManager = CLLocationManager()

我的变量是否放在错误的位置?

import UIKit
import MapKit
import CoreLocation

class InfoViewController: UIViewController,UIGestureRecognizerDelegate, CLLocationManagerDelegate {


    // Outlets
    @IBOutlet weak var infoImageView: UIImageView!


    @IBOutlet weak var nameLabel: UILabel!


    @IBOutlet weak var categoryLabel: UILabel!


    @IBAction func urlButton(sender: UIButton) {
    }

    @IBOutlet weak var mapView: MKMapView!

    // Variables

    var gesture: UISwipeGestureRecognizer!

    var store: Store!

    var locationManager = CLLocationManager()




    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // Background image
        self.infoImageView.image = UIImage(data: store.photo)

        // Set label text
        self.nameLabel.text = store.name

        // Set category text
        var categoryText = String()
        for category in store.storeCategories {
            if !categoryText.isEmpty {
                categoryText += ", "
            }
            categoryText += category.name
        }

        categoryLabel.text = categoryText




        if (CLLocationManager.locationServicesEnabled())
        {

            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()

        }

        func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
            let location = locations.last as! CLLocation

            let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

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

        // Gesture recoginizer
        gesture = UISwipeGestureRecognizer(target: self, action: "dismissView")
        self.gesture.direction = UISwipeGestureRecognizerDirection.Up

        self.view.addGestureRecognizer(self.gesture)

    }

    func dismissView() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }


}

最佳答案

您必须在 block 之外声明 locationManager,在本例中,您尝试在 if-block 内声明它。这将使它成为一个局部变量。在 if-block. 之外声明变量该作业的工作示例代码:

var locationManager = CLLocationManager()

override func viewDidLoad() {

如您所见,我已将变量声明为与 viewDidLoad() 相同的级别。方法,赋予它更大的范围。

然后,在 if block 中您可以执行以下操作:

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

请注意,我删除了 locationManager = CLLocationManager() 的第一行因为它已经实例化了。

此外,locationManager 的函数不得位于 viewDidLoad 方法内部。

此外,我不明白为什么你的if-block正在包装该方法。

关于swift - 在 map View 上使用轨迹位置时出现变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163352/

相关文章:

返回未定义的 Javascript 对象方法?

ios - 使用 corelocation 框架计算速度显示在模拟器中而不是在 device-ios8 中

ios - 如何在 iPhone 稍微滚动时停止 CLLocationManager 标题值更改。

iOS : Region monitoring and battery usage

swift - 在 Swift 中将 Firebase 的一个子节点从一个节点更改为另一个节点

algorithm - 纬度和经度作为多边形算法中点的坐标

ios - 在 GADBannerView 内调整图像大小

mysql - 在 MySQL 中存储 Vector3

ios - 按下完成后停止 AV 播放器

ios - 如何从 ActionSheet 更改 UIButton 的文本