在Swift中使用注释 - .addAnnotation()不工作

时间:2015-09-26 02:10:30

标签: ios swift swift2 mapkit

我正在使用MapKit,我收到以下错误:

enter image description here

即使我把它作为参数,locations.count?也不起作用。它说它不允许Int作为参数。我不太清楚为什么这不起作用。

import UIKit
import MapKit

class MapViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var myMapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let italy = MKPointAnnotation()
        italy.coordinate = CLLocationCoordinate2D(latitude: 41.8947400, longitude: 12.4839000)
        italy.title = "Rome, Italy"

        let england = MKPointAnnotation()
        england.coordinate = CLLocationCoordinate2D(latitude: 51.5085300, longitude: -0.1257400)
        england.title = "London, England"

        let norway = MKPointAnnotation()
        norway.coordinate = CLLocationCoordinate2D(latitude: 59.914225, longitude: 10.75256)
        norway.title = "Oslo, Norway"

        let spain = MKPointAnnotation()
        spain.coordinate = CLLocationCoordinate2D(latitude: 40.41694, longitude: -3.70081)
        spain.title = "Madrid, Spain"

        let locations = [italy, england, norway, spain]

        myMapView.addAnnotation(locations)
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pinIdentifier")
        pin.canShowCallout = true
        return pin
    }
}

1 个答案:

答案 0 :(得分:3)

应该是:

myMapView.addAnnotations(locations) //note the "s"