gpt4 book ai didi

ios - 将地点的网站和价格水平添加到我的自定义类别(Google 地点、Swift)

转载 作者:行者123 更新时间:2023-11-30 11:59:38 27 4
gpt4 key购买 nike

有了这个类

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"

class QPlace: NSObject {

var location: CLLocationCoordinate2D?
var name: String?
var photos: [QPhoto]?
var vicinity: String?
var isOpen: Bool?
var types: [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]

// 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 name = name {
s.append("\(name)")
}

if let vicinity = vicinity {
s.append("Vicinity: \(vicinity)")
}

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

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

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)
}


}

我获得了该地点的详细信息(我正在与 google place 合作),现在我还想添加网站和价格水平,但我不知道该怎么做,我查看了文档 https://developers.google.com/places/web-service/detailsand尝试自己添加,但我无法让它们工作。例如网站,我尝试以 var website: URL? 的形式添加它,并且也在 var website: String? 中添加它,两者都不起作用。那么我该如何在我的类(class)中添加这些地点的详细信息呢?

更新

import Foundation
import UIKit
import CoreLocation
import Alamofire

class NearbyPlaces {
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "bar", image: UIImage(named: "bar_button.png")!), QCategoryy(name :"night_club", image: UIImage(named: "nightclub_button.png")!), QCategoryy(name: "restaurant", image: UIImage(named: "restaurant_button.png")!), QCategoryy(name: "gym", image: UIImage(named: "gym_button.png")!), QCategoryy(name: "spa", image: UIImage(named: "spa_button.png")!), QCategoryy(name: "museum", image: UIImage(named: "museum_button.png")!)]
return list
}

static let searchApiHost = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
static let googlePhotosHost = "https://maps.googleapis.com/maps/api/place/photo"
static let detailsApiHost = "https://maps.googleapis.com/maps/api/place/details/json"

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

Alamofire.request(searchApiHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in

if let error = response.error {
completion(nil, error)
}
if let response = QNearbyPlacesResponse(dic : response.result.value as? [String : Any]) {
completion(response, nil)
}
else {
completion(nil, QNearbyPlacesResponseError.noParsingDone)
}
}
}

static func googlePhotoURL(photoReference:String, maxWidth:Int) -> URL? {
return URL.init(string: "\(googlePhotosHost)?maxwidth=\(maxWidth)&key=\(AppDelegate.googlePlacesAPIKey)&photoreference=\(photoReference)")
}
}

enum QNearbyPlacesResponseError : Error {
case noParsingDone
}


struct QNearbyPlacesResponse {
var nextPageToken: String?
var status: String = "NOK"
var places: [QPlace]?

init?(dic:[String : Any]?) {
nextPageToken = dic?["next_page_token"] as? String

if let status = dic?["status"] as? String {
self.status = status
}

if let results = dic?["results"] as? [[String : Any]]{
var places = [QPlace]()
for place in results {
places.append(QPlace.init(placeInfo: place))
}
self.places = places
}
}

func canLoadMore() -> Bool {
if status == "OK" && nextPageToken != nil && nextPageToken?.characters.count ?? 0 > 0 {
return true
}
return false
}



}

最佳答案

在你的初始化函数中,尝试

print(placeInfo["website"])

如果打印 nil,那么你做错了什么,否则你将得到所需的类型。此外,您需要将 placeInfo["website"] 向下转换为该类型。最有可能的是字符串类型。

// In declaration
var website: String?
// In init
website = placeInfo["website"] as? String

这应该有效。

最后,您提供了类引用的链接,而您似乎正在使用 API 来获取数据。

编辑(评论后):-

您正在使用的 API 未提供您要求的信息。因此要获取信息,您需要使用this API 。您正在调用的 API 中获取 ID,这是调用详细信息 API 所必需的。

希望这能解决您的问题。

关于ios - 将地点的网站和价格水平添加到我的自定义类别(Google 地点、Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47409278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com