gpt4 book ai didi

ios - 如何遍历 JSON 坐标并将注释构建为一个函数?

转载 作者:行者123 更新时间:2023-11-28 06:50:34 24 4
gpt4 key购买 nike

我正在努力获取使用 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)

}
}

最佳答案

我已经对您的代码进行了下面的 soem 修改

  • 没有将 json 转换为 NSArray(使用 .array 而不是 .arrayObject)
  • 移动了在 for 循环内向 map 添加注释以添加所有注释。
  • 将区域设置移至 for 循环之外的 map ,并留给您设置您喜欢的位置。

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

关于ios - 如何遍历 JSON 坐标并将注释构建为一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35037784/

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