Swift 添加对象数组作为地图注释

Swift add array of objects as map annotations

我从我的 CloudKit 数据库中获取了一组对象,这些对象作为 location() 对象数组返回。

location() 定义为...

class location {
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D
}

我需要能够从数组创建注释,但我似乎无法理解它。

当我尝试将数组作为 MKAnnotation 传递时,它说它没有正确符合,尽管它确实具有符合要求的所有数据,但我只是不知道如何将它从数组和注释。

我的class

class Locations: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D

override init()
{
    self.title = "Test Title"
    self.subtitle = "Test Subtitle"
    self.coordinate = CLLocationCoordinate2D.init()
}
}

在我的视图控制器中创建了 "objects"... var objects = [Locations]()

这是我用来从 CK 获取数据并将其存储在对象中的函数的一部分...

for locations in results! {
                let newLocation = Locations()
                newLocation.title = ckData["Name"] as? String
                newHaunted.subtitle = ckData["Subtitle"] as? String
                let location = ckData["Location"] as! CLLocation

                self.objects.append(newLocation)

最后,我在 ViewDidAppear 中调用了一个具有以下代码的函数...

    let locationsToAdd = objects
    mapView.showAnnotations(locationsToAdd, animated: true)

此时我从对象中得到一个空数组。如果我尝试使用 Locations() 而不是对象,它说它不能将其转换为 MKAnnotation,它应该已经是。

以下是我用来从CloudKit获取数据的函数。

func getRecordsFromCloud() {
// Fetch data using Convenience API
let cloudContainer = CKContainer.defaultContainer()
let publicData = cloudContainer.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Locations", predicate: predicate)

publicData.performQuery(query, inZoneWithID: nil) { results, error in
    if error == nil { //no error
        for locations in results! {
            let newLocation = Locations()
            newLocation.title = locations["Name"] as? String
            newLocation.subtitle = locations["Subtitle"] as? String
            let location = locations["Location"] as! CLLocation

            let newLocationCoords: CLLocationCoordinate2D = location.coordinate
            newLocation.coordinate = newLocation
            self.objects.append(newHaunted)


            dispatch_async(dispatch_get_main_queue(), {() -> Void in
                self.locationsTable.reloadData()
            })
        }
    }
    else {
        print(error)
    }
}
}

之后我在 viewDidLoad 中调用 getRecordsFromCloud()。

要指定您的 class 符合特定协议,您必须使用 class ClassName: ProtocolName 表示法。因此,在您的情况下,您应该将 class location 替换为 class location: MKAnnotation 以告诉编译器您的 class 符合 MKAnnotation 协议。

您可以使用@Jelly 的建议或只是声明您的 class 符合(实现)MKAnnotation 扩展名:

extension Location : MKAnnotation 
{
}

您可以这样定义 Location class:

class Location: NSObject, MKAnnotation {
    let title: String?
    let subtitle: String?
    var coordinate: CLLocationCoordinate2D

    init(title: String?, subtitle: String?, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate

        super.init()
    }
}

然后您可以像这样向您的地图添加注释:

let annotation = Location(title: name, subtitle: locality, coordinate: coordinate)
mapView.addAnnotation(annotation)

显然,您可以根据需要设置 titlesubtitle,但希望这能说明这个想法。