首页 > 解决方案 > ios web places api 获取评分和评论

问题描述

我正在尝试通过 google web places 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()
    }

标签: iosswiftgoogle-mapsgoogle-places-api

解决方案


推荐阅读