-1

I have following code:

func addAnnotation(gesture: UIGestureRecognizer){

    //touch point
    var touchPoint = gesture.locationInView(self.UImapMK)
    var location = UImapMK.convertPoint(touchPoint, toCoordinateFromView: self.UImapMK)

    var cllocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
    var addressStr: String = "Undefined"

    //GeoCoder
    CLGeocoder().reverseGeocodeLocation(cllocation, completionHandler: { (placemark, error) -> Void in
        if error == nil {
            if placemark.count > 0{
                var address = placemark[0] as! CLPlacemark
                addressStr = "\(address.locality), \(address.administrativeArea)"
            }
        }
        else{
            addressStr = "Error"
        }

        println("RUN: CLgeocoder")
    })

    //Annotation
    var annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = addressStr

    UImapMK.addAnnotation(annotation)
}

However reverseGeocodeLocation function seems to run after addAnnotation, so every time I have addressStr as "undefined".

Is there a way to make reverseGeocodeLocation run first? Thanks!

Lio Esis
  • 13
  • 3

1 Answers1

1

This sort of thing comes up over and over and over. Move all the code that adds the annotation inside your completion block.

Duncan C
  • 128,072
  • 22
  • 173
  • 272