1

我很难在我的 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.
    }




}
4

1 回答 1

0

我不再有这个问题。我的代码可能与上面略有不同,所以我粘贴了初始化 reverseGeocodeLocation 的代码部分:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

let locationManager = CLLocationManager()

var longitude: Double!
var latitude: Double!

var thoroughfare = "unknown"
var subLocality = "unknown"
var subAdministrativeArea = "unknown"
var postCode = "unknown"
var locality = "unknown"

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    latitude = userLocation.coordinate.latitude
    longitude = userLocation.coordinate.longitude

    let userLocation:CLLocation = locations[0]

    CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in

        if(error == nil) {
            let loc = placemarks![0]

            self.thoroughfare = String!(loc.thoroughfare)
            self.subLocality = String!(loc.subLocality)
            self.subAdministrativeArea = String!(loc.subAdministrativeArea)
            self.postCode = String!(loc.postalCode)
            self.locality = String!(loc.locality)
    } else {
            //print(error)
        }
        print("thoroughfare: " + self.thoroughfare)
    }
}
于 2015-12-16T01:04:33.897 回答