/ /地図上の複数のJSONデータを瞬時に3つ表示して、緯度と経度を表示しますか? - json、swift3、mapkit

スワイプ3でマップ内の複数のJSONデータを使用して緯度と経度を表示しますか? - json、swift3、mapkit

私は複数のJSONデータを表示することができませんMapViewでAlamofireを使用して緯度と経度を表示します.Normal Static ArrayはMapViewに表示されますが、動的データはMapに表示されません。この問題はどのように解決できますか?

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

}
}
}

ここに画像の説明を入力

なぜ私は動的データを表示することができません。 解決しましたか?

回答:

回答№1は0

あなたのコードを以下のコードに置き換えてください:

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)

}

あなたの中でこれを解放する getschoolMapJson()

これがうまくいくことを願って