Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift CLLOCATIONCoordinated2D到一个数组中_Swift_Dictionary_Mapkit_Coordinates_Cllocationcoordinate2d - Fatal编程技术网

Swift CLLOCATIONCoordinated2D到一个数组中

Swift CLLOCATIONCoordinated2D到一个数组中,swift,dictionary,mapkit,coordinates,cllocationcoordinate2d,Swift,Dictionary,Mapkit,Coordinates,Cllocationcoordinate2d,我有一个动态数量的位置被绘制到地图工具包。我很好奇如何将我当前的纬度和经度放到一个数组中,因为它们目前被打印为单独的对象,而不是像它应该绘制的那样绘制地图。我知道这个问题,但不知道如何解决。这是我当前生成坐标的代码- do { let place = try myContext.executeFetchRequest(fetchRequest) as! [Places] for coords in place{ let latarra

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

  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)")
    }
这是控制台上的指纹-

我需要的是正确打印的样子-


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

您可以创建
CLLocationCoordinate2D
s的数组:

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)
    }
}

请显示比这一行更多的代码,这一行创建了一个坐标,而不是任何类型的数组。@luk2302:它不会创建一个坐标,正如您可以看到的,输出图像将用户输入的核心数据中的位置作为坐标保存。我已经用更多的代码更新了这篇文章。现在的问题是,每次都会创建一个数组,因为核心数据中有大量的位置。这张图显示了这一点。当前存储了4个位置,最后一个数组显示在输出中。我怎么才能避开这个问题呢?愚蠢的错误,我把打印放在了循环之外,所有的显示都是正确的!非常感谢。我仍然有线绘制的问题,但我现在可以前进了。再次感谢。@rscode101没问题,只要记住当您对问题的答案感到满意时,请将其标记为已回答。我遇到了另一个问题,也许您可以帮助我解决。在创建MKPointAnnotion时,我如何使用刚刚创建的存储数组坐标,因为我遇到了此错误,无法将“[CLLocationCoordinate2D]”类型的值分配给“CLLocationCoordinate2D”类型有什么想法吗@dzkThe
locations
现在是一个数组,因此您需要从中选择一个对象分配给每个
MKPointAnnotation
,我将更新上面的答案。
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
    }
}