ich diese Klasse erstelltder Website Ort Downloaden und Preisstufen (Google Orte, Swift)
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)
}
}
an den Ort, um die Details zu bekommen, die ich func getDescription() -> String { }
durch Aufruf dieser Funktion in einem VC meiner Anwendung zeigen werde aber mein Problem und ich weiß nicht warum (weil ich der Dokumentation von Google Places API folge) ist, dass die Website und auch das Preisniveau nicht funktioniert, so wenn in der ViewController ich die Funktion getDescription()
rufen Sie laden alle Details des Ortes, die ich in diesem hinzugefügt Funktion, aber nicht die Website und das Preisniveau. Was habe ich falsch gemacht? Wie kann ich es einstellen?
UPDATE
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()
]
}
aber du denkst, ich habe die Website korrekt in meiner Klasse hinzugefügt, das Format ist diese Website = placeInfo [websiteKey] als? Schnur? –
Wenn Sie andere Werte sehen, scheint die Art der Implementierung der Website korrekt zu sein. Warum versuchen Sie nicht, die rohe JSON vor dem Parsen zu drucken, um zu sehen, ob Sie Website-Wert von Google API – carbonr
Ich habe versucht, price_level zu verwenden und suchte viele Orte, aber das gleiche Ergebnis, nicht laden –