4

我将动态数量的位置绘制到 mapkit 上。我很好奇如何将当前的纬度和经度放入一个数组中,因为它们当前被打印为单独的对象,而这些对象并未像应有的那样绘制地图。我知道这个问题,但不知道如何解决它。这是我当前生成坐标的代码 -

  do {
        let place = try myContext.executeFetchRequest(fetchRequest) as! [Places]

        for coords in place{
            let latarray = Double(coords.latitude!)
            let lonarray = Double(coords.longitude!)
            let arraytitles = coords.title!

            let destination:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latarray, lonarray)

        print(destination)

    } catch let error as NSError {
        // failure
        print("Fetch failed: \(error.localizedDescription)")
    }

这是控制台中的打印 - 输出

我需要打印的东西才能正常工作 -期望的输出

我希望你明白我的意思。我非常感谢任何帮助!感谢您的阅读。

4

1 回答 1

4

您可以创建一个 s 数组CLLocationCoordinate2D

var coordinateArray: [CLLocationCoordinate2D] = []

if latarray.count == lonarray.count {
    for var i = 0; i < latarray.count; i++ {
        let destination = CLLocationCoordinate2DMake(latarray[i], lonarray[i])
        coordinateArray.append(destination)
    }
}

编辑:

在您的代码中,数组latarray也不是。lonarray如果你想创建一个CLLocationCoordinate2Ds 数组,你应该添加一个变量来存储你的位置,你的 for 循环应该如下所示:

var locations: [CLLocationCoordinate2D] = []

for coords in place{
    let lat = Double(coords.latitude!)
    let lon = Double(coords.longitude!)
    let title = coords.title!

    let destination = CLLocationCoordinate2DMake(lat, lon)
    print(destination) // This prints each location separately

    if !locations.contains(destination) {
        locations.append(destination)
    }
}

print(locations) // This prints all locations as an array

// Now you can use your locations anywhere in the scope where you defined the array.
func getLocationFromArray() {
    // Loop through the locations array:
    for location in locations {
        print(location) // Prints each location separately again
    }
}
于 2016-02-08T19:11:26.633 回答