大家好,我想从3个数组中获取数据以创建地图注释。但是我无法将数据从这3个数组加载到一个类。这是我的代码:
我的班级文件:
import MapKit
class Jobdata: NSObject, MKAnnotation {
let title: String?
let coordinate: CLLocationCoordinate2D
init(title: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.coordinate = coordinate
super.init()
}
}
这些是我的数组:
var jobNameST = [String]()
var jobLongitudeST = [Double]()
var jobLatitudeST = [Double]()
这是我的位置数组:
let jobLocations = [Jobdata(title: "test", coordinate: CLLocationCoordinate2D(latitude: 30.4692991035765, longitude: -97.7660876))]
我想将这3个数组添加到我的locations数组中。
答案 0 :(得分:0)
首先,您需要创建位置模型 我希望所有数组都有相同数量的记录 请先检查是否可以
struct Location
{
var title: String?
var latitude: Double?
var longitude: Double?
init(title: String, latitude: Double, longitude: Double) {
self.title = title
self.latitude = latitude
self.longitude = longitude
}
}
根据给定的标题,纬度和经度数组创建位置模型数组
func getLocations() -> [Location] {
var locations = [Location]()
for (index, value) in jobNameST.enumerated() {
let location = Location(title: value, latitude: jobLatitudeST[index], longitude: jobLongitudeST[index])
locations.append(location)
}
return locations
}