开发者社区> 问答> 正文

在SWIFT中使用Google Place Photos请求显示图像

我正在尝试使用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请求,并显示返回的照片?

展开
收起
游客5akardh5cojhg 2019-12-09 13:25:17 466 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
OpenStack Swift 海量小文件优化之路 立即下载
From Java/Android to Swift iOS 立即下载
Swift在Airbnb的应用实践 立即下载