swift Google Places API -返回相同结果的下一页令牌

nukf8bse  于 2023-02-11  发布在  Swift
关注(0)|答案(2)|浏览(157)

我正在使用Google Places API在一个表格视图中输出餐馆的名称。我已经能够解析JSON并获得这些地方的名称和图片。但是,表格视图只包含20个结果。因此,我发现我需要使用提供的next_page_token来获得其余数据。但是,当我获得next_page_token并将其放入请求URL中时,来自第一查询的数据在表格视图内重复多次。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    var location : CLLocation? = locations.last

    var searchURL = NSString(format: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=50.940463,-114.077553&radius=50000&type=restaurant&key=MY_API_KEY") as? String

    callAlamo(url: searchURL!)
}    

func callAlamo(url:String){

    Alamofire.request(url).responseJSON(completionHandler: { response in

        self.parseData(JSONData: response.data!)
    })
}

// PARSING JSON DATA FOR GETTING NAME AND PICTURE OF PLACES AND GETTING LATITUDE AND LONGITUDE

func parseData(JSONData:Data){

    do{
        var myReadableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard

        // PARSING THROUGH JSON DATA TO GET NAMES AND PICTURES OF PLACES, THEN PUTTING 
        // THEM INTO AN ARRAY AND OUTPUTTING THEM ONTO TABLE VIEW CELL

        if let results = myReadableJSON["results"] as? [JSONStandard]{
            for i in 0..<results.count{

                let item = results[i]
                print("Item is: ",item)
                let names = item["name"] as! String

                placeNames.append(names)

                // GETTING PHOTO URL WITH photo_reference AND PUTTING THEM INTO imageURL ARRAY

                if let photos = item["photos"] as? [JSONStandard]{

                    for k in 0..<photos.count{

                        let photo = photos[k] as JSONStandard

                        let photoRef = photo["photo_reference"] as! String

                        let photoURL = NSString(format: "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=%@&key=MY_API_KEY", photoRef) as? String
                        imageURL.append(photoURL!)
                    }
                }

                if let geometry = item["geometry"] as? JSONStandard{
                    if let location = geometry["location"] as? [String : Any]{

                            let latitude = location["lat"] as? Double
                            let longitude = location["lng"] as? Double
                    }
                }
            }

            // CHECK TO SEE IF JSON DATA CONTAINS next_page_token. IF IT DOES, REPEAT PROCESS OF 
            // PARSING THROUGH JSON DATA AND GET SECOND PAGE OF DATA. REPEAT UNTIL LAST PAGE
            // DOESN'T CONTAIN next_page_token.

            if (myReadableJSON["next_page_token"] != nil){

                nextPageToken = myReadableJSON["next_page_token"] as! String
                let nextPageGoogleURL = NSString(format:"https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=%@&key=MY_API_KEY",nextPageToken ) as String
                DispatchQueue.main.async {

                    self.callAlamo(url: nextPageGoogleURL)

                }
            }
            // SHOULD BE PLACED AT THE END OF GATHERING DATA
            locationManager.stopUpdatingLocation()
            self.tableView.reloadData()
            imageURL.removeAll()
            placeNames.removeAll()
        }
    }
    catch{
        print(error)
    }
}
    • 更新日期:**

我能够找到解决我问题的方法。多亏了stack overflow上的另一个线程。
我只需要在parseData()函数中添加一个if let条件,但要将其置于循环之外,并添加一个定时器延迟,正如在所提供的SO查询的答案中提到的,从发出next_page_token到您可以访问该数据之间存在一个时间延迟,因此我添加了一个轻微的时间延迟。

func parseData(JSONData:Data){

    do{
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard

        placeNames.removeAll()

        if let results = readableJSON["results"] as? [JSONStandard]{
            print("JSON: ",results)
            for i in 0..<results.count{

                let item = results[i]
                let names = item["name"] as! String

                placeNames.append(names)

                if let geometry = item["geometry"] as? JSONStandard{
                    if let location = geometry["location"] as? [String : Any]{

                         self.latitude = (location["lat"] as? Double)!
                         self.longitude = (location["lng"] as? Double)!
                    }
                }

                let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
                marker.icon = UIImage(named: "locationMarker")
                print(placeNames[i],"\n")
                marker.title = placeNames[i]
                marker.snippet = "Nothing"
                marker.map = self.googleMapsContrainer
            }
        }

        if let pageToken = readableJSON["next_page_token"]{

            let newURL = NSString(format:"https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=\(pageToken)&key=MY_API_KEY" as NSString) as? String

            let when = DispatchTime.now() + 2
            DispatchQueue.main.asyncAfter(deadline: when, execute: { 

                self.callAlamo(url: newURL!)
            })

        }
    }
    catch{
        print(error)
    }
}`
alen0pnh

alen0pnh1#

理想情况下,google最多返回20个位置的结果(每次搜索最多可以返回60个结果),如果搜索包含20个以上的结果,那么你将在你的响应对象中得到*next_page_token***,如果next_page_token为nil或没有返回,那么没有进一步的结果。**
样本响应对象

"html_attributions" : [],
   "next_page_token" : "CqQCEQEAANi9ziRMdWd-lHhBE1Mhr6S7wqt_97liE5q42J3heMg6XqL2vwdOClJa5_YzdZmtLmhe_QFimH6y6a0dUXSXRYMXXMG2lemTCoQYYhRF7py9tafUrqILe5spsUBTMDbI9-mJUjY548lH0c7oX8NH-V3KH4VuDRA-6opxF0_IG5Z0bbzPz9-q2OhmkW9IS3w_rH1jKtTJiOSYljM6ZSJh7jSZi_dx76qtN8hxhE4trrFKLGO1WfHmjBWo506Qi5bANLikIs0qtkbYzBqu2AjYIJp-dpA7rAVWLKHWUi7Vs6oEYI_L_iqr7JODDF4XkTyjg2U_2rPxbgRR8RV6GUt-rZ1R8nEJLVrYA0LBYsZdJd",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8709434,
               "lng" : 151.1903114
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8694072197085,
                  "lng" : 151.1917756802915
               },
               "southwest" : {
                  "lat" : -33.8721051802915,
                  "lng" : 151.1890777197085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "e644f7f34cf875b9919c6548f1b721947362850a",
         "name" : "Lunch Cruise with Jazz on Sydney Harbour",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 292,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/105423912060796272053/photos\"\u003eLunch Cruise with Jazz on Sydney Harbour\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAA0X0ax8-an2n_73VHZ-oHXfnJMId06BSZwJWwTqGPL2FtzZIIt--PyUhyEByzew0dyAS85yAPWn-gPdVfMOuPnHiThkRMPntdbWSRcIA9Vm9vAqQx9RWfVBg21Xf2ePfAEhCfYg0Rz46AvyL4XopycJJeGhQh_eFhP7pQXd1LGezkVW4gpat3AA",
               "width" : 438
            }
         ],
         "place_id" : "ChIJLfySpTOuEmsRPCRKrzl8ZEY",
         "rating" : 3,
         "reference" : "CmRRAAAAACfGiWX6r6IcOaFJBLN1VY3XdxLD4zUufz7sjPHMfIdzGa4ZycRoZyCJbGis42HZnxLC5b28TbMvfaVsDBcck5DDuv39sdq4w2q1dU0Z5rtmFGsXeFaRrdo36jIMlMEaEhC3grmYSOAnDyGektJCIxbrGhQwyyGsW0OiItrqFtKzazRcEDE-EQ",
         "scope" : "GOOGLE",
         "types" : [
            "night_club",
            "bar",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "37 Bank Street, Pyrmont"
      },
.
.
.
"status": "OK"
}`

分页请求的示例url应该包含pagetoken作为参数https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key=YOUR_API_KEY&pagetoken=Xyz
参考Google places Api

fdx2calv

fdx2calv2#

对于其他可能发现这个问题的人,在我的例子中,我犯了一个错误,即发送next_page_token=作为url参数,但是正确的发送参数是pagetoken=

相关问题