ios web将api获得评分和评论

时间:2018-05-29 16:23:48

标签: ios swift google-maps google-places-api

我想通过谷歌网站api

获得评分和评论

通过地方详细信息我可以访问地点评分和评论

我希望能够显示地图上显示的所有地点的评分,但地方详细信息仅提供一个我可以使用的地方ID吗?

现在我找到了该地区的所有星巴克,并返回一些信息,如开放时间。我希望它能够在搜索后显示类似于谷歌地图显示的评级评论。

func loadStarbucksJson(_ latLong:(Double,Double)) {
        isLoading = true
        NearbyStarbucks.sharedInstance.stores.removeAll()
        tableView.reloadData()
        activityIndicator.startAnimating()

        var keys : NSDictionary?
        if let path = Bundle.main.path(forResource: "keys", ofType: "plist") {
            keys = NSDictionary(contentsOfFile: path)
        }
        var apiKey : String = ""
        if let dict = keys {
            apiKey = dict["webPlacesApiKey"]! as! String
        }
        let url = URL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(latLong.0),\(latLong.1)&rankby=distance&type=cafe&keyword=Starbucks&key=\(apiKey)")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if let data = data {
                do {
                    // Convert the data to JSON
                    let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
                    if let json = jsonSerialized, let results = json["results"] {

                        for result in (results as! NSArray as! [NSDictionary]) {
                            if let geometry = result["geometry"] as! NSDictionary?, let location = geometry["location"] as! NSDictionary? {
                                let name = result["name"] as! String
                                let vicinity = result["vicinity"] as! String
                                let lat = location["lat"] as! Double
                                let lng = location["lng"] as! Double

                              // let rating = result["rating"] as! String
                                var openNow : Bool? = nil
                                if let opening = result["opening_hours"] as! NSDictionary?{
                                    if let opened = opening["open_now"] as! Bool? {
                                        openNow = opened
                                    }
                                }
                                let newStore = Store(name: name, vicinity: vicinity, lat: lat, lng: lng, openNow: openNow)
                                NearbyStarbucks.sharedInstance.stores += [newStore]
                            }
                        }
                    }
                } catch let error as NSError {
                    print(error.localizedDescription)
                }

            } else if let error = error {
                print(error.localizedDescription)
                let alertController = UIAlertController(title: "Could not load data", message: "Please connect to the Internet.", preferredStyle: UIAlertControllerStyle.alert)
                alertController.addAction(UIAlertAction(title: "Retry", style: UIAlertActionStyle.default, handler: { (alert) in
                    self.loadStarbucksJson(latLong)
                }))
                self.present(alertController, animated: true, completion: nil)
            }
            self.postLoadStoreJson()
        }
        task.resume()
    }

0 个答案:

没有答案