Swift HTTP Request to Google Elevation API



我试图向Google Elevation API发送请求,我已经成功发送了单个请求。GE API的文档中指出,如果以"|"分隔的列表传递坐标,则单个请求可以包含许多坐标计算。

正如你在我的代码中看到的,我遵循的格式是请求包含这样的东西>>3.222,54.333 | 2.444, 66.4332 |…

这应该是工作从我可以告诉,但显然有些地方是错误的。

这是一段代码,程序在这段代码的错误点处中断。

finalStringConvertedCoordinates = processedQueryPoints.joined(separator: "|")
let apiKey = "REDACTED"
guard let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?key=(apiKey)&locations=(finalStringConvertedCoordinates)") else {
print("Error: cannot create URL")
return

在弄清楚processsedquerypoints:

中的内容后,尝试这样做
let locations = ["3.222,54.333", "2.444,66.4332"]
let locs = locations.joined(separator: "%7C")  // <-- the important bit
print("n---> locs: (locs)")
let apiKey = "REDACTED"
let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?locations=(locs)&key=(apiKey)")
print("---> url: (url) n")

编辑:以字符串数组开头,如下所述:

let rawData = ["3.222","54.333", "2.444","66.4332"]
print("n---> rawData: (rawData)")

let pairs = stride(from: 0, to: rawData.endIndex, by: 2).map {(rawData[$0], rawData[$0.advanced(by: 1)])}
print("n---> pairs: (pairs)")

let locs = pairs.map{ $0 + "," + $1 }.joined(separator: "%7C")
print("n---> locs: (locs)")

let apiKey = "REDACTED"
let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?locations=(locs)&key=(apiKey)")
print("---> url: (url) n")

最新更新