我使用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)
}
}`