此代码不向 mapView 添加注释。我在一个答案中看到,每次调用 addAnotation
时都会调用 mapView
函数,那么问题出在哪里?但是当我移动 map 时,它们会出现。
func addPlacesMarkers(location:CLLocation) {
self.communication.getJsonData(self.getPubsNearByCreator(location)) { (finalData, error) -> Void in
if error != nil {
print(error)
} else {
if let row: NSArray = finalData {
for var i = 0; i < row.count; i++ {
let lat = row[i]["lat"] as! String
let lng = row[i]["lng"] as! String
let title = row[i]["name"] as! String
let id = row[i]["id"] as! String
let point = CustomizedAnotation(id: Int(id)!, name: title)
point.title = title
point.coordinate.latitude = Double(lat)!
point.coordinate.longitude = Double(lng)!
let keyExists = self.places[Int(id)!] != nil
if keyExists == false {
self.places.updateValue(point, forKey: Int(id)!)
}
}
var finalPlaces :[MKPointAnnotation] = []
for place in self.places.values {
finalPlaces.append(place)
}
self.mView.addAnnotations(finalPlaces)
self.mView.showsPointsOfInterest = false
}
}
}
}
最佳答案
您不能在与主线程不同的线程中修改 UI。 您应该将 UI 修改代码放在 dispatch_async block 中,如下所示:
dispatch_async(dispatch_get_main_queue()) {
//Your code that modify the UI
self.mView.addAnnotations(finalPlaces)
}
关于ios - Swift 在另一个线程的 mapView 中显示注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548796/