如何遍历 JSON 坐标并将注释构建为一个函数?
Posted
技术标签:
【中文标题】如何遍历 JSON 坐标并将注释构建为一个函数?【英文标题】:How do I iterate through JSON co-ordinates and build annotations as one function? 【发布时间】:2016-01-27 12:45:20 【问题描述】:我正在努力获取使用 JSON 数据放置的注释。我尝试将 JSON 中的坐标迭代到一个新数组中,但是当我尝试将一个数组传递到我需要坐标的位置时它失败了,因为它不能接受数组。我该如何解决这个问题?
谁能帮忙?
Alamofire.request(.GET, "https://demo1991046.mockable.io/score/locations").responseJSON (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["users"].arrayObject as? [NSArray]
self.newArray = (resData as? [NSArray])
print("\([self.newArray])")
for var i = 0; i < self.newArray!.count; ++i
self.longitude.append(self.newArray[i]["lon"] as! String!)
print("longitude: \(self.longitude)")
self.latitude.append(self.newArray[i]["lat"] as! String!)
print("latitude: \(self.latitude)")
let doubleLat = self.latitude.map
Double(($0 as NSString).doubleValue)
let doubleLon = self.longitude.map
Double(($0 as NSString).doubleValue)
print("doublelat: \(doubleLat)")
print("doubleLon: \(doubleLon)")
// 1
self.locationManager.delegate = self
// 2
self.locationManager.requestAlwaysAuthorization()
// 3
let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: doubleLat, longitude: doubleLon) // <- here is where I get an error: "Cannot convert value of type '[Double]' to expect argument type 'CLLocationDegrees' (aka 'Double")"
// print("lat: \((locationManager.location?.coordinate.latitude)!)")
// print("lon: \((locationManager.location?.coordinate.longitude)!)")
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
self.mapView.setRegion(theRegion, animated: true)
let anotation = MKPointAnnotation()
anotation.coordinate = location
anotation.title = "The Location"
anotation.subtitle = "This is the location !!!"
self.mapView.addAnnotation(anotation)
【问题讨论】:
从我在你的代码中看到的doubleLat
和 doubleLon
是 [Double]
不是双倍,这就是 let location:...
行中的错误的原因
【参考方案1】:
我已经对您的代码进行了以下一些修改
未将 json 转换为 NSArray(使用.array
而不是 .arrayObject
)
已将注释添加到 for 循环内的地图以添加所有注释。
将区域设置移至 for 循环之外的地图,并留给您设置您喜欢的位置。
Alamofire.request(.GET, "https://demo1991046.mockable.io/score/locations").responseJSON (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
// get the users from the json var, no need to convert it to Array
guard let usersJsonArray = swiftyJsonVar["users"].array else
// users not found in the json
return
// the usersJsonArray is array of json which will be much easier for work with.
// No need for 1,2 and 3 to be in the for loop.
// 1
self.locationManager.delegate = self
// 2
self.locationManager.requestAlwaysAuthorization()
// 3
let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
for userJson in usersJsonArray
let longitudeString = userJson["lon"].stringValue
print("longitude: \(longitudeString)")
let latitudeString = userJson["lat"].stringValue
print("latitude: \(latitudeString)")
let doubleLat = Double(latitudeString)
let doubleLon = Double(longitudeString)
print("doublelat: \(doubleLat)")
print("doubleLon: \(doubleLon)")
// by having the next code block inside the for loop you will be able to add all the user locations to the map as anotations.
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: doubleLat, longitude: doubleLon) // Now should work fine
let anotation = MKPointAnnotation()
anotation.coordinate = location
anotation.title = "The Location"
anotation.subtitle = "This is the location !!!"
self.mapView.addAnnotation(anotation)
// for usersJson
// you need to figure out the loaction you will set for the mapView region.
let location = .... // set the location you like.
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
self.mapView.setRegion(theRegion, animated: true)
【讨论】:
@AndrewWormald 欢迎您。你应该对这个问题进行任何编辑吗?我想投票,但在您进行任何编辑之前不能投票以上是关于如何遍历 JSON 坐标并将注释构建为一个函数?的主要内容,如果未能解决你的问题,请参考以下文章
遍历数组,并将子 JSON 转换为 observablearray?
循环遍历数组,并将子JSON转换为observablearray?
如何循环遍历动态大小的数组并将属性作为参数传递给可变参数函数?