下载该地方的网站和priceLevel(Google地方,Swift)

时间:2017-11-14 10:53:31

标签: ios swift xcode swift3 google-places-api

我创建了这个类

import UIKit
import CoreLocation

private let geometryKey = "geometry"
private let locationKey = "location"
private let latitudeKey = "lat"
private let longitudeKey = "lng"
private let nameKey = "name"
private let openingHoursKey = "opening_hours"
private let openNowKey = "open_now"
private let vicinityKey = "vicinity"
private let typesKey = "types"
private let photosKey = "photos"
private let phoneNumberKey = "phoneNumber"
private let ratingKey = "rating"
private let priceLevelKey = "priceLevel"
private let websiteKey = "website"


class QPlace: NSObject  {

    var location: CLLocationCoordinate2D?
    var name: String?
    var photos: [QPhoto]?
    var vicinity: String?
    var isOpen: Bool?
    var types: [String]?
    var rating: Float?
    var priceLevel: Int?
    var website: String?

    init(placeInfo:[String: Any]) {
        // coordinates
        if let g = placeInfo[geometryKey] as? [String:Any] {
            if let l = g[locationKey] as? [String:Double] {
                if let lat = l[latitudeKey], let lng = l[longitudeKey] {
                    location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
                }
            }
        }





        // name
        name = placeInfo[nameKey] as? String

        // opening hours
        if let oh = placeInfo[openingHoursKey] as? [String:Any] {
            if let on = oh[openNowKey] as? Bool {
                isOpen = on
            }
        }

        // vicinity
        vicinity = placeInfo[vicinityKey] as? String

        // types
        types = placeInfo[typesKey] as? [String]

        // rating
        rating = placeInfo[ratingKey] as? Float

        //priceLevel
        priceLevel = placeInfo[priceLevelKey] as? Int

        website = placeInfo[websiteKey] as? String

        // photos
        photos = [QPhoto]()
        if let ps = placeInfo[photosKey] as? [[String:Any]] {
            for p in ps {
                photos?.append(QPhoto.init(photoInfo: p))
            }
        }
    }

    func getDescription() -> String {

        var s : [String] = []

        if let types = types {
            s.append("\(types.joined(separator: ", "))")
        }

        if let rating = rating {
            s.append("Rating: \(rating)")
        }

        if let priceLevel = priceLevel {
            s.append("PriceLevel: \(priceLevel)")
        }

        if let website = website {
            s.append("\(website)")
        }

         if let isOpen = isOpen {
            s.append(isOpen ? "OPEN NOW" : "CLOSED NOW")
        }

        if let vicinity = vicinity {
            s.append("\(vicinity)")
        }
        return s.joined(separator: "\n")
    }


 func heightForComment(_ font: UIFont, width: CGFloat) -> CGFloat {
        let desc = getDescription()
        let rect = NSString(string: desc).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
        return ceil(rect.height)
    }


}

通过调用此函数func getDescription() -> String { }获取我将在我的应用程序的VC中显示的地方的详细信息,但我的问题和我不知道为什么(因为我按照google places API的文档)是该网站以及价格水平不起作用,所以当在viewController中我调用函数getDescription()时,它会加载我在该函数中添加的所有地方的详细信息,而不是网站和价格水平。我做错了什么?我该如何调整呢?

更新

https://developers.google.com/places/ios-api/place-details

static func getNearbyPlaces(by category:String, coordinates:CLLocationCoordinate2D, radius:Int, token: String?, completion: @escaping (QNearbyPlacesResponse?, Error?) -> Void) {

        var params : [String : Any]

        if let t = token {
            params = [
                "key" : AppDelegate.googlePlacesAPIKey,
                "pagetoken" : t,
            ]
        } else {
            params = [
                "key" : AppDelegate.googlePlacesAPIKey,      
                "radius" : radius,
                "location" : "\(coordinates.latitude),\(coordinates.longitude)",
                "type" : category.lowercased()
            ]
        }

1 个答案:

答案 0 :(得分:2)

根据Google Places API

价格水平的关键是price_level,但您使用了priceLevel,这可能是您没有看到价格的原因

至于网站,我认为可能是您所选择的房产没有可用数据。