reverseGeocodeLocation:“无法使用类型'(地标:CLPlacemark)'的参数列表调用类型'CLPlacemark'的初始化程序'”

时间:2015-08-09 15:43:46

标签: swift2 cllocation xcode7-beta4

我对如何在reverseGeocodeLocation尝试中初始化此地标感到难过。

首先,我不知道reverseGeocodeLocation是否正确初始化。它给了我一些问题;但是Xcode似乎在这一点上没问题。

真正的问题出现在第55行: placemark = CLPlacemark(地标:placemarkArr [0] as!CLPlacemark)

我收到错误:“无法使用类型为'(地点标记: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.
    }




}

1 个答案:

答案 0 :(得分: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)
    }
}