我无法在mapView.Normal中使用Alamofire显示多个带有纬度和经度的JSON数据。静态数组在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)
}
}
}
}
为什么我无法显示动态数据。这个问题怎么样? 解决了吗?
答案 0 :(得分:0)
使用以下代码替换您的代码:
try {
Integer.parseInt(answer);
} catch (NumberFormatException e) {
throw new InputMismatchException("Enter Numbers");
}
在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)
}
希望这会起作用