我正在尝试使用Google的PlaceAPI提供的“照片_引用”来显示特定位置的照片。
目前,我正在使用GooglePlaceAPI的“地点细节“请求获取有关特定地点的信息,但在显示这些地点的照片时遇到了困难。”Google在“PlaceDetails”响应中提供了一个“PhotoReference”,这个响应应该用来获取特定的图像,但是文档这里在展示如何做到这一点上没有多大帮助。
我目前正在制作Google Place Details请求,如下所示:(我有一个函数来创建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中的“PhotoReference”来制作GooglePlacePhoto请求,并显示返回的照片?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
看起来你已经正确地构造了URL来获取地点的详细信息,包括照片引用。为了从“PhotoReference”中实际获取图片,你需要使用Google Places API的“Photo”服务。这个服务允许你通过提供之前获得的“PhotoReference”来下载照片。
这里是一个基本的示例,展示如何构建请求URL以获取特定照片:
func googlePlacePhotoURL(forKey apiKey: String, photoReference: String, maxWidth: Int? = nil, maxHeight: Int? = nil) -> URL {
let baseURL = "https://maps.googleapis.com/maps/api/place/photo?"
let photoReferenceString = "photoreference=" + photoReference
var sizeParam = ""
if let maxWidth = maxWidth, let maxHeight = maxHeight {
// 选择宽度或高度中的较小值来保持图片比例
let size = min(maxWidth, maxHeight)
sizeParam = "maxwidth=\(size)&maxheight=\(size)"
} else if let maxWidth = maxWidth {
sizeParam = "maxwidth=\(maxWidth)"
} else if let maxHeight = maxHeight {
sizeParam = "maxheight=\(maxHeight)"
}
let key = "key=" + apiKey
let fullURLString = baseURL + photoReferenceString + "&" + sizeParam + "&" + key
print("Photo request URL:", fullURLString)
return URL(string: fullURLString)!
}
在你的代码中,一旦你从“PlaceDetails”响应中获得了“photo_reference”,你可以像这样调用上面的函数来获取照片的URL:
if let photoReferences = placeDetailsResult["photos"] as? [[String: Any]], let firstPhoto = photoReferences.first, let photoReference = firstPhoto["photo_reference"] as? String {
if let photoURL = googlePlacePhotoURL(forKey: yourAPIKey, photoReference: photoReference, maxWidth: 500) {
// 使用这个URL加载或显示图片,例如在UIImageView中
// 这里只是一个示例,具体实现取决于你的应用需求
print("Photo URL:", photoURL.absoluteString)
}
}
请确保替换yourAPIKey
为你的实际Google API密钥,并根据需要调整maxWidth
和maxHeight
参数。然后,你可以使用返回的URL来加载或显示图片。