【问题标题】:Updating MapView based on Geocoded addresses, only one pin of many locations showing根据地理编码地址更新 MapView,仅显示多个位置中的一个引脚
【发布时间】: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


    【解决方案1】:

    在 myGroup.notify(queue: .main) 方法中完成循环后尝试编写以下代码。

    self.mapView?.showAnnotations((self.mapView?.annotations)!, animated: true)
    

    【讨论】:

    • 嗯,好像没用。我认为问题是 myGroup.notify(queue: .main) 只被调用一次,而不是每个地理编码线程
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2012-11-02
    • 2012-10-06
    相关资源
    最近更新 更多