gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:12:36 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com