CLLocationCoordinate2D 到一个数组
CLLocationCoordinate2D into one array
我在 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)")
}
这是控制台中的打印 -
Output
我需要打印件才能正常工作 - Desired output
希望你明白我的意思。我非常感谢任何帮助!感谢阅读。
您可以创建一个 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
都不是数组。如果你想创建一个 CLLocationCoordinate2D
的数组,你应该添加一个变量来存储你的位置,你的 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
}
}
我在 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)")
}
这是控制台中的打印 - Output
我需要打印件才能正常工作 - Desired output
希望你明白我的意思。我非常感谢任何帮助!感谢阅读。
您可以创建一个 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
都不是数组。如果你想创建一个 CLLocationCoordinate2D
的数组,你应该添加一个变量来存储你的位置,你的 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
}
}