簡體   English   中英

reverseGeocodeLocation:“無法為類型為'(placemark:CLPlacemark)'的參數列表調用類型'CLPlacemark'的初始化程序”

[英]reverseGeocodeLocation: “Cannot invoke initializer for type 'CLPlacemark' with an argument list of type '(placemark: CLPlacemark)'”

我對如何在reverseGeocodeLocation嘗試中初始化此地標感到困惑。

首先,我不知道reverseGeocodeLocation是否正確初始化。 它給了我一些問題; 但是Xcode在這一點上似乎還可以。

真正的問題出在第55行:placemark = CLPlacemark(placemark: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.
    }




}

我不再遇到這個問題。 我的代碼可能與上面的代碼略有不同,因此我在代碼的下面粘貼了以下內容,以初始化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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM