Geocoder a list of data from array just show the last data on mapview

24 Views Asked by At

I am new in coding and i am blocked facing this issue. I try to make a loop to geocode all data from my table "restaurants" and show them on map view but when i launch my application only the last data is shown. How can i show all my list on the map ? Many thanks for your help

let 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)


                }

            }

        }
        )
    }
0

There are 0 best solutions below