swift MapKit没有显示注释引脚。

时间:2021-08-11 21:17:08

I'm learning swift and trying to use SwiftyJson to parse a json file and add annotations in the map view but couldn't get the pins showed on the simulator. I have a warning in the debugging area says that Could not inset legal attribution from corner 4. My code is as below and I've checked some of the answers about this problem but still couldn't fix it. Any help is greatly appreciated.

我正在学习swift,并尝试使用SwiftyJson解析json文件并在map视图中添加注释,但无法获得模拟器上显示的大头针。我在调试区域有一个警告说不能从角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 个解决方案

#1


1  

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

check this.

检查这个。

#2


0  

You need to call addAnnotation in fetchJsonData(), because fetchJsonData() is executed asynchronously.

您需要在fetchJsonData()中调用addAnnotation,因为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

#1


1  

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

check this.

检查这个。

#2


0  

You need to call addAnnotation in fetchJsonData(), because fetchJsonData() is executed asynchronously.

您需要在fetchJsonData()中调用addAnnotation,因为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