gpt4 book ai didi

ios - 在 Swift 中使用 Google Place Photos 请求显示图像

转载 作者:行者123 更新时间:2023-11-28 20:49:38 25 4
gpt4 key购买 nike

我正在尝试使用 Google Places API 提供的“photo_reference”来显示特定位置的照片。

目前,我正在使用 Google Places API 的“Place Details” ' 请求获取有关特定位置的信息,但无法显示这些位置的照片。 Google 在“地点详细信息”响应中提供了“photo_reference”,该响应应该用于获取特定图像,但文档 here对展示如何做不是很有帮助。

我目前正在像这样发出 Google 地点详情请求:(我有一个创建 url 的函数,然后是一个发出请求的函数)

    func googlePlacesDetailsURL(forKey apiKey: String, place_ID: String) -> URL {
print("passed place_ID before url creation ", place_ID)

let baseURL = "https://maps.googleapis.com/maps/api/place/details/json?"
let place_idString = "place_id=" + place_ID
let fields = "fields=geometry,name,rating,price_level,types,opening_hours,formatted_address,formatted_phone_number,website,photos"
let key = "key=" + apiKey

print("Details request URL:", URL(string: baseURL + place_idString + "&" + fields + "&" + key)!)
return URL(string: baseURL + place_idString + "&" + fields + "&" + key)!
}


func getGooglePlacesDetailsData(place_id: String, using completionHandler: @escaping (GooglePlacesDetailsResponse) -> ()) {

let url = googlePlacesDetailsURL(forKey: googlePlacesKey, place_ID: place_id)

let task = session.dataTask(with: url) { (responseData, _, error) in
if let error = error {
print(error.localizedDescription)
return
}

guard let data = responseData, let detailsResponse = try? JSONDecoder().decode(GooglePlacesDetailsResponse.self, from: data) else {
print("Could not decode JSON response. responseData was: ", responseData)
return
}
print("GPD response - detailsResponse.result: ", detailsResponse.result)

completionHandler(detailsResponse)

}
task.resume()

}

有谁知道如何在 Swift 中使用“photo_reference”发出 Google Place Photo 请求并显示返回的照片?

最佳答案

据我了解,您似乎必须使用从地点搜索或地点详情请求返回的 Photo_reference 创建一个新的 URL。

正如您认为 photo_reference 不是图像,而是可以用来访问图像的 Http 引用,使用该引用,您必须替换下面的字符串并访问该链接以返回图像

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=YOUR_API_KEY

“Your_Api_Key”显然是它的意思,您必须访问返回的照片引用并使用 replaceCharactersInRange 并替换下面链接中的“photoreference”。

maxwidth 可以根据您需要的图像大小进行设置。

关于ios - 在 Swift 中使用 Google Place Photos 请求显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240115/

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