How do i return coordinates after forward geocoding?

You are correct about the asynchronous issue. Basically, you cannot do anything after this code:

// [A1]
self.geocoder.geocodeAddressString(combinedAddress, completionHandler: {
    (placemarks, error) -> Void in
    // [B] ... put everything _here_
})
// [A2] ... nothing _here_

The reason is that the stuff inside the curly braces (B) happens later than the stuff outside it (including the stuff afterward, A2). In other words, the code in my schematic above runs in the order A1, A2, B. But you are dependent on what happens inside the curly braces, so you need that dependent code to be inside the curly braces so that it executes in sequence with the results of the geocoding.

Of course this also means that the surrounding function cannot return a result, because it returns before the stuff in curly braces has even happened. The code in my schematic goes A1, A2, return! Only later does B happen. So clearly you cannot return anything that happens in B because it hasn’t happened yet.

Leave a Comment