将坐标字符串转换为 Swift 中的 CLLocations 数组
Convert string of coordinates into an array of CLLocations in Swift
我有一个包含坐标列表的字符串,我需要将其转换为数组。我试着做 let array = Array(coordinates)
但它说 String
不再符合 SequenceType
。我需要将字符串转换为 CLLocations
的数组。我尝试转换的字符串如下所示:
let coordinates = "[[39.86475483576405,-75.53281903266907], [39.864688955564304,-75.53292632102966], [39.86455719497505,-75.53300142288208], [39.86440072894666,-75.5330228805542], [39.8642689678039,-75.53295850753784], [39.863305456757146,-75.53223967552185], [39.86303369478483,-75.53266882896423]]"
一种方法是去掉所有的括号和空格,然后将逗号分隔的数字拆分成一个数组。然后将每对数字字符串转换成数字,最后从这对数字中创建一个CLLocation
。
// Your string
let coordinates = "[[39.86475483576405,-75.53281903266907], [39.864688955564304,-75.53292632102966], [39.86455719497505,-75.53300142288208], [39.86440072894666,-75.5330228805542], [39.8642689678039,-75.53295850753784], [39.863305456757146,-75.53223967552185], [39.86303369478483,-75.53266882896423]]"
// Remove the brackets and spaces
let clean = coordinates.replacingOccurrences(of: "[\[\] ]", with: "", options: .regularExpression, range: nil)
// Split the comma separated strings into an array
let values = clean.components(separatedBy: ",")
var coords = [CLLocation]()
for i in stride(from: 0, to: values.count, by: 2) {
// Pull out each pair and convert to Doubles
if let lat = Double(values[i]), let long = Double(values[i+1]) {
let coord = CLLocation(latitude: lat, longitude: long)
coords.append(coord)
}
}
该字符串是有效的 JSON 字符串。
最直接的方法是将字符串反序列化为JSONSerialization
并将结果映射到[CLLocation]
let coordinates = "[[39.86475483576405,-75.53281903266907], [39.864688955564304,-75.53292632102966], [39.86455719497505,-75.53300142288208], [39.86440072894666,-75.5330228805542], [39.8642689678039,-75.53295850753784], [39.863305456757146,-75.53223967552185], [39.86303369478483,-75.53266882896423]]"
if let data = coordinates.data(using: .utf8),
let jsonArray = try? JSONSerialization.jsonObject(with: data) as? [[Double]] {
let locationArray = jsonArray!.map{CLLocation(latitude:[=10=][0], longitude:[=10=][1]) }
print(locationArray)
}
我有一个包含坐标列表的字符串,我需要将其转换为数组。我试着做 let array = Array(coordinates)
但它说 String
不再符合 SequenceType
。我需要将字符串转换为 CLLocations
的数组。我尝试转换的字符串如下所示:
let coordinates = "[[39.86475483576405,-75.53281903266907], [39.864688955564304,-75.53292632102966], [39.86455719497505,-75.53300142288208], [39.86440072894666,-75.5330228805542], [39.8642689678039,-75.53295850753784], [39.863305456757146,-75.53223967552185], [39.86303369478483,-75.53266882896423]]"
一种方法是去掉所有的括号和空格,然后将逗号分隔的数字拆分成一个数组。然后将每对数字字符串转换成数字,最后从这对数字中创建一个CLLocation
。
// Your string
let coordinates = "[[39.86475483576405,-75.53281903266907], [39.864688955564304,-75.53292632102966], [39.86455719497505,-75.53300142288208], [39.86440072894666,-75.5330228805542], [39.8642689678039,-75.53295850753784], [39.863305456757146,-75.53223967552185], [39.86303369478483,-75.53266882896423]]"
// Remove the brackets and spaces
let clean = coordinates.replacingOccurrences(of: "[\[\] ]", with: "", options: .regularExpression, range: nil)
// Split the comma separated strings into an array
let values = clean.components(separatedBy: ",")
var coords = [CLLocation]()
for i in stride(from: 0, to: values.count, by: 2) {
// Pull out each pair and convert to Doubles
if let lat = Double(values[i]), let long = Double(values[i+1]) {
let coord = CLLocation(latitude: lat, longitude: long)
coords.append(coord)
}
}
该字符串是有效的 JSON 字符串。
最直接的方法是将字符串反序列化为JSONSerialization
并将结果映射到[CLLocation]
let coordinates = "[[39.86475483576405,-75.53281903266907], [39.864688955564304,-75.53292632102966], [39.86455719497505,-75.53300142288208], [39.86440072894666,-75.5330228805542], [39.8642689678039,-75.53295850753784], [39.863305456757146,-75.53223967552185], [39.86303369478483,-75.53266882896423]]"
if let data = coordinates.data(using: .utf8),
let jsonArray = try? JSONSerialization.jsonObject(with: data) as? [[Double]] {
let locationArray = jsonArray!.map{CLLocation(latitude:[=10=][0], longitude:[=10=][1]) }
print(locationArray)
}