ios - How do I iterate through JSON co-ordinates and build annotations as one function? -
i struggling annotations being placed using json data. have tried iterating coordinates json new array when try pass array need coordinates fails because cannot take arrays. how can fix this?
can help?
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])") var = 0; < 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 nsstring).doublevalue) } let doublelon = self.longitude.map { double(($0 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 error: "cannot convert value of type '[double]' 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 location !!!" self.mapview.addannotation(anotation) } }
i have done soem modifies below code
- didn't convert json nsarray (by using
.array
instead of.arrayobject
) - moved adding anotation map inside loop add of them.
- moved setting region map out side loop , left set location like.
alamofire.request(.get, "https://demo1991046.mockable.io/score/locations").responsejson { (responsedata) -> void in let swiftyjsonvar = json(responsedata.result.value!) // users json var, no need convert array guard let usersjsonarray = swiftyjsonvar["users"].array else { // users not found in json return } // usersjsonarray array of json easier work with. // no need 1,2 , 3 in loop. // 1 self.locationmanager.delegate = self // 2 self.locationmanager.requestalwaysauthorization() // 3 let thespan:mkcoordinatespan = mkcoordinatespanmake(0.01 , 0.01) 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)") // having next code block inside loop able add user locations map anotations. let location:cllocationcoordinate2d = cllocationcoordinate2d(latitude: doublelat, longitude: doublelon) // should work fine let anotation = mkpointannotation() anotation.coordinate = location anotation.title = "the location" anotation.subtitle = "this location !!!" self.mapview.addannotation(anotation) } // usersjson // need figure out loaction set mapview region. let location = .... // set location like. let theregion:mkcoordinateregion = mkcoordinateregionmake(location, thespan) self.mapview.setregion(theregion, animated: true) }
Comments
Post a Comment