/ / Breite und Länge mit mehreren JSON-Daten in Map in swift 3 anzeigen? - json, swift3, mapkit

Breite und Länge mit mehreren JSON-Daten in Map in swift 3 anzeigen? - json, swift3, mapkit

Ich bin nicht in der Lage, mehrere JSON-Daten mit anzuzeigenBreite und Länge mit Alamofire in mapView.Normal Statisches Array wird in MapView angezeigt, aber Dynamische Daten werden nicht in Map angezeigt. Wie kann dieses Problem gelöst werden?

import UIKit
import MapKit
import Alamofire
import SwiftyJSON
class NearbyViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

@IBOutlet weak var mapview: MKMapView!
@IBOutlet weak var name: UILabel!

var schoolMap : [schools] = []
var scholmap : schools?

let locations = [
["title": "New York, NY",    "latitude": 40.713054, "longitude": -74.007228],
["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344],
["title": "Chicago, IL",     "latitude": 41.883229, "longitude": -87.632398]
]
override func viewDidLoad() {
super.viewDidLoad()
getschoolMapJson()
getmapSchool()


self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

}

override  func viewWillAppear(_ animated: Bool) {
mapview.reloadInputViews()
}
override func viewDidAppear(_ animated: Bool) {
mapview.reloadInputViews()
}

func getmapSchool(){

for locationn in schoolMap{
let location = CLLocationCoordinate2DMake((locationn.latitude) , (locationn.longitude))
print("lati",locationn.longitude)
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = locationn.name
print("namelati",locationn.name)
mapview.addAnnotation(annotation)
}
}


func getschoolMapJson(){
if (schoolMap.count > 0){
return
}

let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/schools")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)

/*for i in 0..<self.myresponse.count{
let schools_data = schools(schoolJson:self.myresponse[i])
self.schooldata.append(schools_data)

}*/
for school in myresponse.array!{
let schoolsObj = schools(schoolJson: school)
self.schoolMap.append(schoolsObj)
debugPrint(schoolsObj)
}
//                self.mapview.reloadInputViews()
case.failure(let error):
print("Not Success",error)
}

}
}
}

Bildbeschreibung hier eingeben

Warum kann ich die dynamischen Daten nicht anzeigen? Wie kann dieses Problem sein? gelöst?

Antworten:

0 für die Antwort № 1

Ersetzen Sie Ihren Code durch den folgenden Code:

for school in myresponse.array!{
let schoolsObj = schools(schoolJson: school)

//Add Pin
let location = CLLocationCoordinate2DMake((schoolsObj.latitude) , (schoolsObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = locationn.name
mapview.addAnnotation(annotation)

self.schoolMap.append(schoolsObj)

}

Platziere dies in deinem getschoolMapJson()

Hoffe, das wird funktionieren