ios - 下载地点的网站和 priceLevel(Google places,Swift)

标签 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()
            ]
        }

最佳答案

根据 Google Places API :

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

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

关于ios - 下载地点的网站和 priceLevel(Google places,Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47283851/

相关文章:

ios - uicollectionview - 数据自动加载而不调用 reloaddata

iphone - 使用randNum无法访问NSArray中的对象

ios - 从 super View 中删除的自动布局不会更新

ios - 如何在 iOS 中设置 GCKUIMiniMediaControlsViewController 的样式?

ios - Apple iOS 分发证书在重置后有一个新的私钥

swift - SpriteKit : Add Multiple SKSpriteNodes To The Scene

ios - 在 TableView 中选择行时看到相应的 View Controller

Xcode 构建步骤以验证 Cocoapods 是否是最新的

objective-c - 使应用程序的更新版本与其以前的版本兼容

ios - SKEffectNode 不会在 Swift 中使用自定义 CIFilter