'CLLocation' 不可转换为 'CLLocationCoordinate2D'

'CLLocation' is not convertible to 'CLLocationCoordinate2D'

我正在尝试利用多个教程自学 Swift。

到目前为止我有这段代码。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate
{
    @IBOutlet weak var myMapView: MKMapView!

    var manager:CLLocationManager!
    var myLocations: [CLLocation] = []

    @IBOutlet weak var textView: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        //Setup our Location Manager
        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()

        textView.delegate = self

        //Setup our Map View
        myMapView.delegate = self
        myMapView.mapType = MKMapType.Satellite
        myMapView.showsUserLocation = true
    }

    func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
        //theLabel.text = "\(locations[0])"
        myLocations.append(locations[0] as CLLocation)

        let spanX = 0.007
        let spanY = 0.007
        var newRegion = MKCoordinateRegion(center: myMapView.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
        myMapView.setRegion(newRegion, animated: true)

        if (myLocations.count > 1){
            var sourceIndex = myLocations.count - 1
            var destinationIndex = myLocations.count - 2

            let c1 = myLocations[sourceIndex].coordinate
            let c2 = myLocations[destinationIndex].coordinate
            var a = [c1, c2]
            var polyline = MKPolyline(coordinates: &a, count: a.count)
            myMapView.addOverlay(polyline)
        }

    }
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        var annotation = CustomAnnotation(coordinate: manager.location, title: textView.text, subtitle: "SubTitle");
        myMapView.addAnnotation(annotation)
        textView.resignFirstResponder()
        return true
    }
}

这一行

var annotation = CustomAnnotation(coordinate: manager.location, title: textView.text, subtitle: "SubTitle");

导致此错误:

'CLLocation' is not convertible to 'CLLocationCoordinate2D'

我的另一个 Swift 文件是:

import UIKit
import MapKit

class CustomAnnotation: NSObject, MKAnnotation {

    var coordinate:CLLocationCoordinate2D
    var title:NSString!
    var subtitle: NSString!

    init(coordinate: CLLocationCoordinate2D, title: NSString!, subtitle: NSString!) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }

}

错误信息相当明确。 init 想要一个 CLLocationCoordinate2D 而你正试图给它一个 CLLocation。我怀疑您想传递 manager.locationcoordinate 属性 而不是位置本身。

CLLocation 有一个 属性 名为 "coordinate" 的 CLLocationCoordinate2D 类型,因此您可以访问该字段并获取 CLLocation

的 CLLocationCoordinate2D