ViewController的基本思想是显示餐厅的名称及其评级星。餐厅的名称来自我在JSON中的API,评级来自Google Places API。
我首先从服务器获取名称:
func getData (){
let urlStr = "http://myserver.com/api/"
let url = URL(string: urlStr)
Alamofire.request(url!, method: .get ,encoding: URLEncoding.default).responseJSON { response in
if let value : AnyObject = response.result.value as AnyObject {
let shops = JSON(value)
for (key,subJson):(String, JSON) in shops["families"] {
if subJson["disable"] == "0"{
let shopName = subJson["name"].stringValue
self.performSereach(shopName: shopName)//do search
// take the name and
var info = Shops(shopname: shopName,
Logo: logoString, family_id: familiy_id , design : designImage)
// save the data to show in TableVIewCell
现在,我在Google地方使用链接进行搜索
https://maps.googleapis.com/maps/api/place/textsearch/json?query=\(shopName)+in+SanFrancisco&key=MyKey
进行搜索的功能:
func performSereach(shopName : String ) {
let formattedString = shopName.replacingOccurrences(of: " ", with: "+")
let urlStr = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=\(formattedString)+in+ SanFrancisco&key=MyKey"
Alamofire.request(urlStr).responseJSON { response in
if let value : AnyObject = response.result.value as AnyObject {
let data = JSON(value)
for (key,subJson):(String, JSON) in data["results"] {
print(subJson["rating"])
print(subJson["name"])
但是当我这样做时,我觉得我做错了,因为:
1-我所在区域有很多星巴克,所以回复将有多个评级(在这种情况下我想采取第一个)并且打印报表打印所有星巴克及其评级
2-如何在Shops
搜索结束时将shopName与其评级星连接?
Shops
是:
import Foundation
class Shops {
var _familiy_id: String?
var _logo : String?
var _design: String?
var _rate : Int
var _shopname : String?
var rate : Int{
return _rate
}
var familiy_id : String{
return _familiy_id!
}
var shopname : String{
return _shopname!
}
var Logo : String{
return _logo!
}
var design : String {
return _design!
}
init(shopname : String , Logo : String , family_id : String, design : String , rate : Int) {
self._shopname = shopname
self._logo = Logo
self._familiy_id = family_id
self._design = design
self._rate = rate
}
}