ios - 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()
}
解决方案
推荐阅读
- objective-c - 如何确定是否显示 tableViews 页脚?
- javascript - 合并从 API vue.js 获得的数据的问题
- java - 构造函数(我们编写的代码)是否替换了默认构造函数?
- android - 单击推送通知 Firebase 不起作用
- java - intellij - 无法解析符号“爱立信”
- excel - 如何解决 vba 运行时错误'-2147286788 (800300fc)
- sql-server - SQL Server 2014 在多边形中找到一个点
- locking - Apache Ignite 锁到期
- python - DataGenerator(Sequence) - 如何检查 batch_x 和 batch_y.shape?
- inno-setup - 使用 Inno Setup 设置 DisableFullScreenOptimization 应用程序兼容性标志