【发布时间】:2017-06-11 07:17:16
【问题描述】:
我无法等待异步调用,然后将结果地址填充到 MKMapView 的地标中。
问题是只有一个地标最终显示在地图视图上。我知道其他人存在,因为我已经调试过它并在数组中看到了这种情况。它只是没有完全填充地图视图/刷新分配给它的MKAnnotation。
// Load user sales
myFirebaseController.loadSales(){(result: [SaleModel]) in
// Assign result
self.salesList = result
let geocoder = CLGeocoder()
let myGroup = DispatchGroup()
// For each sale loaded in
for sale in self.salesList{
myGroup.enter()
// Decode the address into coordinates
geocoder.geocodeAddressString(sale.saleAddress!, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error!)
}
if let placemark = placemarks?.first {
// Set the coordinate
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
// Add the annotation
self.createMapAnnotation(coordinate: coordinates, title: sale.saleTitle!, address: sale.saleAddress!)
}
myGroup.leave()
})
}
myGroup.notify(queue: .main){
for annotation in self.mapView.annotations {
self.mapView.removeAnnotation(annotation)
self.mapView.addAnnotation(annotation)
}
}
}
正如您所看到的,一旦离开调度组,我尝试删除所有注释并重新添加它们,作为刷新它的一种手段,尽管我认为这是一个糟糕的尝试,显然它不会改变地图视图(只记录self.salesList的第一个sale注解)
我在这里做错了什么?
注意:self.createMapAnnotation() 为地图视图添加注释。
编辑:我尝试遵循这个 - Wait until swift for loop with asynchronous network requests finishes executing 但它似乎对我的情况没有帮助
【问题讨论】:
标签: ios swift mkmapview mapkit mapkitannotation