gpt4 book ai didi

json - 在 Swift 3 中解析 JSON (CLLocationCoordinate2D)

转载 作者:行者123 更新时间:2023-11-28 14:44:11 25 4
gpt4 key购买 nike

我正在尝试使用 Google Places iOS API 解析 JSON 链接。当我打印坐标时,它显示为

CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)

而不是地点的实际坐标。我认为这可能是一个字典/对象问题,但找不到解决方案。

下面是我的JSON解析代码:

func parseJsonData(data: NSData) -> [GymGooglePlace] {

do {

typealias JSONDictionary = [String:Any]

if let parsedData = try JSONSerialization.jsonObject(with: data as Data) as? JSONDictionary, let gyms = parsedData["results"] as? [JSONDictionary] {

for gym in gyms {

var coordinate = CLLocationCoordinate2D()
var latitude:CLLocationDegrees!
var longitude:CLLocationDegrees!

var maxWidth:Int!
var photoReference:String!
let photoURL:String!

if let geometry = gym["geometry"] as? [JSONDictionary] {

for result in geometry {

if let locations = result["location"] as? [JSONDictionary] {

for location in locations {

latitude = location["lat"] as! CLLocationDegrees!
longitude = location["lng"] as! CLLocationDegrees!

coordinate.latitude = latitude
coordinate.longitude = longitude
}
}
}
}

if let photos = gym["photos"] as? [JSONDictionary] {

for photo in photos {

maxWidth = photo["width"] as! Int!
photoReference = photo["photo_reference"] as! String!
}
}

photoURL = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=\(maxWidth!)&photoreference=\(photoReference!)&key=\(googleAPIKey)"

print(photoURL)
print(coordinate)
print("spacespacespacespacespacespacespacespacespace")
}
}



} catch {
print(error)
}

return gymGooglePlaces
}

最佳答案

geometrylocation的值是字典,请注意{},数组是[]

 if let geometry = gym["geometry"] as? JSONDictionary,
let location = geometry["location"] as? JSONDictionary {
coordinate.latitude = location["lat"] as! CLLocationDegrees
coordinate.longitude = location["lng"] as! CLLocationDegrees
}

关于json - 在 Swift 3 中解析 JSON (CLLocationCoordinate2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50376961/

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