gpt4 book ai didi

ios - 如何通过 google maps iOS API 对地址进行地理编码?

转载 作者:IT王子 更新时间:2023-10-29 05:39:05 25 4
gpt4 key购买 nike

我找到了一种发送请求的方法:

A Google Maps Geocoding API request takes the following form:

https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters where outputFormat may be either of the following values:

json (recommended) indicates output in JavaScript Object Notation (JSON); or xml indicates output in XML To access the Google Maps Geocoding API over HTTP, use:

但是确实很不方便,请问swift有没有native的方式?

我查看了 GMSGeocoder 接口(interface),发现它的 API 只能进行反向地理编码。

最佳答案

正如其他人所指出的,没有预定义的搜索方法,但您可以使用网络请求访问 Google Geocoding API你自己:

func performGoogleSearch(for string: String) {
strings = nil
tableView.reloadData()

var components = URLComponents(string: "https://maps.googleapis.com/maps/api/geocode/json")!
let key = URLQueryItem(name: "key", value: "...") // use your key
let address = URLQueryItem(name: "address", value: string)
components.queryItems = [key, address]

let task = URLSession.shared.dataTask(with: components.url!) { data, response, error in
guard let data = data, let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, error == nil else {
print(String(describing: response))
print(String(describing: error))
return
}

guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
print("not JSON format expected")
print(String(data: data, encoding: .utf8) ?? "Not string?!?")
return
}

guard let results = json["results"] as? [[String: Any]],
let status = json["status"] as? String,
status == "OK" else {
print("no results")
print(String(describing: json))
return
}

DispatchQueue.main.async {
// now do something with the results, e.g. grab `formatted_address`:
let strings = results.compactMap { $0["formatted_address"] as? String }
...
}
}

task.resume()
}

关于ios - 如何通过 google maps iOS API 对地址进行地理编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com