谷歌放置网络服务:如何在Swift 3中获得下一页的结果

时间:2017-05-12 16:08:17

标签: ios swift google-api

我在iOS应用中使用Google Web Service API查找用户所在位置附近的临终关怀位置。我可以获得结果的第一页,但是使用pagetoken检索下一页结果是不成功的。以下是我的搜索功能。任何有关我出错的地方的帮助(以前从未使用过URLSession)将不胜感激。

func performGoogleQuery(url:URL)
{
    print("PERFORM GOOGLE QUERY")
    let task = URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in

        if error != nil
        {
            print("An error occured: \(error)")
            return
        }

        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]

            // Parse the json results into an array of MKMapItem objects
            if let places = json?["results"] as? [[String : Any]]
            {
                print("Places Count = \(places.count)")     // Returns 20 on first pass and 0 on second.

                for place in places
                {
                    let name = place["name"] as! String
                    print("\(name)")

                    if let geometry = place["geometry"] as? [String : Any]
                    {
                        if let location = geometry["location"] as? [String : Any]
                        {
                            let lat = location["lat"] as! CLLocationDegrees
                            let long = location["lng"] as! CLLocationDegrees
                            let coordinate = CLLocationCoordinate2DMake(lat, long)
                            let placemark = MKPlacemark(coordinate: coordinate)
                            let mapItem = MKMapItem(placemark: placemark)
                            mapItem.name = name
                            self.mapitems.append(mapItem)

                        }
                    }
                }
                print("mapItems COUNT = \(self.mapitems.count)")    // Remains at 20 after 2 passes.
            }
            // If there is another page of results, 
            // configure the new url and run the query again.
            if let pageToken = json?["next_page_token"]
            {
                let newURL = URL(string: "https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken=\(pageToken)&key=\(self.googleAPIKey)")
                //print("PAGETOKENURL = \(newURL)")

                self.performGoogleQuery(url: newURL!)
            }
        }catch {
            print("error serializing JSON: \(error)")
        }
    })
     task.resume()
}
  

更新(基于Dima的回复):更改   self.performGoogleQuery(url:newURL!)

到这个

let when = DispatchTime.now() + 2 // change 2 to desired number of seconds
            DispatchQueue.main.asyncAfter(deadline: when) {
                        self.performGoogleQuery(url: newURL!)
            }

1 个答案:

答案 0 :(得分:2)

根据documentation:

  

发出next_page_token和之间有一段短暂的延迟   什么时候会有效。

我认为您可能过快地提取下一页。尝试设置至少几秒的延迟,看看是否能解决您的问题。

从我看到的情况来看,我并不认为你是想快速连续自动获取页面。他们似乎希望您让用户触发获取其他内容。

  

您可以在原始页面后最多两次请求新页面   查询。必须依次显示每页结果。两个或两个以上   搜索结果页面不应显示为a的结果   单一查询。