Ios 基于地理编码地址更新MapView,许多位置中只有一个pin显示
我在等待异步调用,然后将结果地址填充到Ios 基于地理编码地址更新MapView,许多位置中只有一个pin显示,ios,swift,mkmapview,mapkit,mapkitannotation,Ios,Swift,Mkmapview,Mapkit,Mapkitannotation,我在等待异步调用,然后将结果地址填充到MKMapView的位置标记时遇到问题 问题是,地图视图上只显示了一个地标。我知道其他的存在,因为我已经调试了它,并在数组中看到了这些。它只是没有完全填充地图视图/刷新分配给它的MKAnnotation // Load user sales myFirebaseController.loadSales(){(result: [SaleModel]) in // Assign result self.salesL
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)
}
}
}
正如您所看到的,一旦dispatch group离开,我会尝试删除所有注释并重新添加它们,作为刷新它的一种方式,尽管我认为这是一个糟糕的尝试,而且显然它不会更改地图视图(它只记录self.salesList
的第一个销售
注释)
我做错了什么
注意:self.createMapAnnotation()
将注释添加到地图视图中
编辑:我尝试了以下操作-但似乎对我的情况没有帮助在myGroup.notify(queue:.main)方法中完成循环后尝试编写以下代码
嗯,好像不管用。我认为问题在于,
myGroup.notify(queue:.main)
只被调用一次,而不是针对每个地理代码线程
self.mapView?.showAnnotations((self.mapView?.annotations)!, animated: true)