0

我是编码新手,我被阻止面对这个问题。我尝试创建一个循环来对我的表“餐厅”中的所有数据进行地理编码,并将它们显示在地图视图上,但是当我启动我的应用程序时,只显示最后一个数据。如何在地图上显示我的所有列表?非常感谢您的帮助

让 geoCoder = CLGeocoder() var i = 0

    while i < restaurants.count-1 {
        i += 1
        geoCoder.geocodeAddressString(restaurants[i].location, completionHandler: {
            placemarks, error in
            if error != nil {
                print(error!)
                return
            }

            if let placemarks = placemarks {
                //Get the first placemarks
                let placemark = placemarks[0]

                //Add annotation
                let annotation = MKPointAnnotation()
                annotation.title = self.restaurants[i].name
                annotation.subtitle = self.restaurants[i].type



                if let location = placemark.location { annotation.coordinate = location.coordinate

                    self.mapView.showAnnotations([annotation], animated: true)
                    self.mapView.selectAnnotation(annotation, animated: true)


                    //set the zoom level
                    let region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 25000, 25000)
                    self.mapView.setRegion(region, animated: false)


                }

            }

        }
        )
    }
4

0 回答 0