swift MapKit没有显示注释引脚

时间:2017-11-07 03:46:22

标签: ios swift annotations mapkit

我正在学习swift并尝试使用SwiftyJson来解析json文件并在地图视图中添加注释,但无法在模拟器上显示引脚。我在调试区域有一个警告,说不能从第4角插入法律归属。我的代码如下,我已经检查了一些关于这个问题的答案,但仍然无法修复它。非常感谢任何帮助。

        class StationsViewController: UIViewController {

    var stations = [Station]()
    @IBOutlet weak var mapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self //as MKMapViewDelegate
        //mapView.showsUserLocation = YES
        fetchJsonData()
        mapView.addAnnotations(stations)
        }
    func fetchJsonData() {
        // Fetching client list.
        let api_json_url = URL(string:"https://feeds.divvybikes.com/stations/stations.json")
        // Create a URL request with the API address
        let urlRequest = URLRequest(url: api_json_url!)
        // Submit a request to get the JSON data
        //let session = URLSession.shared
        let task = URLSession.shared.dataTask(with: urlRequest) {data,response,error in

            // if there is an error, print the error and do not continue
            if error != nil {
                print("Failed to parse")
                return
            }

            // if there is no error, fetch the json formatted content
            else{
                    let json = JSON(data:data!)
                    if let stationJSONs = json["stationBeanList"].array {
                        for stationJSON in stationJSONs {
                            if let station = Station.from(json: stationJSON) {
                                self.stations.append(station)
                    }
                }
            }
        }// end if
    } // end getDataSession

    task.resume()
    } // end readJsonData function
}

2 个答案:

答案 0 :(得分:2)

for stationJSON in stationJSONs {
    if let station = Station.from(json: stationJSON) {
        self.stations.append(station)

        let latitude = station["latitude"]
        let longitude = station["longitude"]

        let annotation = MKPointAnnotation()
        let centerCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude)
        annotation.coordinate = centerCoordinate
        annotation.title = "Pass Title here"
        mapView.addAnnotation(annotation)
    }  
}

检查一下。

答案 1 :(得分:0)

您需要在addAnnotation中致电fetchJsonData(),因为fetchJsonData()是异步执行的。

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self //as MKMapViewDelegate
    //mapView.showsUserLocation = YES
    fetchJsonData()
}

func fetchJsonData() {
    // Fetching client list.
    let api_json_url = URL(string:"https://feeds.divvybikes.com/stations/stations.json")
    // Create a URL request with the API address
    let urlRequest = URLRequest(url: api_json_url!)
    // Submit a request to get the JSON data
    //let session = URLSession.shared
    let task = URLSession.shared.dataTask(with: urlRequest) {data,response,error in

        // if there is an error, print the error and do not continue
        if error != nil {
            print("Failed to parse")
            return
        }

            // if there is no error, fetch the json formatted content
        else{
            let json = JSON(data:data!)
            if let stationJSONs = json["stationBeanList"].array {
                for stationJSON in stationJSONs {
                    if let station = Station.from(json: stationJSON) {
                        self.stations.append(station)
                        mapView.addAnnotation(station)
                    }
                }
            }
        }// end if
    } // end getDataSession

    task.resume()
} // end readJsonData function