【发布时间】:2016-01-25 18:51:28
【问题描述】:
我正在尝试将位置检测添加到应用程序中,并且在大多数情况下我已经弄清楚了。但是我遇到了reverseGeocodeLocation 方法的问题。我收到错误cannot convert value of type 'CLLocation.Type' to expected argument type 'CLLocation'。什么会导致这种情况?这是我的代码:
func loadLocation() {
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = 2000
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.locationManager.stopUpdatingLocation()
var location = locations.first
var geoCoder: CLGeocoder!
geoCoder.reverseGeocodeLocation(location: CLLocation) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
^ Error Here
var placemark: CLPlacemark = placemarks?.first
self.location = placemark.locality
self.location = placemark.administrativeArea
}
}
locationManager 和 location 在 viewDidLoad 之上调用:
var locationManager: CLLocationManager!
var location: String!
override func viewDidLoad() {
super.viewDidLoad()
loadLocation()
retrieveWeatherForecast()
}
如果您发现我的代码有任何其他问题,您也可以指出来。
更新:
在进行了 trojanfoe 建议的更改后,这就是我所拥有的:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.locationManager.stopUpdatingLocation()
let location = locations.first
var geoCoder: CLGeocoder!
geoCoder.reverseGeocodeLocation(location!) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
let placemark: CLPlacemark = (placemarks?.first)!
self.location = placemark.locality
self.location = placemark.administrativeArea
}
}
Xcode 想让我把 var geoCoder: CLGeocoder! 改成 let geoCoder: CLGeocoder!。如果我这样做,我会收到此错误Variable 'geoCoder' captured by a closure before being initialized。我该怎么办?
【问题讨论】:
标签: ios swift core-location cllocation reverse-geocoding