我很难在我的 reverseGeocodeLocation 尝试中初始化这个地标。
首先,我不知道 reverseGeocodeLocation 是否正确初始化。这给了我一些问题;但 Xcode 在这一点上似乎没问题。
真正的问题出现在第 55 行:placemark = CLPlacemark(placemark: placemarkArr[0] as!CLPlacemark)
我收到错误消息:“无法使用类型为 '(place mark: CLPlacemark)' 的参数列表调用类型为 'CLPlacemark' 的初始化程序”
我正在使用 Xcode 7 Beta 4;所以我不知道这是否是问题所在。我看过关于如何编写这行代码的较早示例,但它们似乎都是较早的代码版本。
感谢您的任何帮助或建议!这个问题困扰了我一段时间。
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var manager:CLLocationManager?
@IBOutlet weak var latitude: UILabel!
@IBOutlet weak var longitude: UILabel!
@IBOutlet weak var altitude: UILabel!
@IBOutlet weak var course: UILabel!
@IBOutlet weak var speed: UILabel!
@IBOutlet weak var address: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager!.delegate = self
manager!.desiredAccuracy - kCLLocationAccuracyBest
manager!.requestWhenInUseAuthorization()
manager!.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
let userLocation:CLLocation = locations[0]
self.latitude.text = "\(userLocation.coordinate.latitude)"
self.longitude.text = "\(userLocation.coordinate.longitude)"
self.course.text = "\(userLocation.course)"
self.speed.text = "\(userLocation.speed)"
self.altitude.text = "\(userLocation.altitude)"
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(userLocation) { (placemarkArr, error) -> Void in
var placemark:CLPlacemark!
//let gp = userLocation
if error == nil && placemarkArr!.count > 0 {
print(userLocation)
placemark = CLPlacemark(placemark: placemarkArr[0] as! CLPlacemark)
var addressString: String = ""
if placemark.ISOcountryCode == "TW" {
if placemark.country != nil {
addressString = placemark.country!
}
if placemark.subAdministrativeArea != nil {
addressString = addressString + placemark.subAdministrativeArea! + ", "
}
if placemark.postalCode != nil {
addressString = addressString + placemark.postalCode! + " "
}
if placemark.locality != nil {
addressString = addressString + placemark.locality!
}
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}