I've got weird problem. Im trying to geocode address from string and it works but I can only get data inside geocoder function. My code:
private func getLocationFromAddress(address: String) -> CLLocationCoordinate2D{
let geocoder = CLGeocoder()
var lat = CLLocationDegrees()
var long = CLLocationDegrees()
geocoder.geocodeAddressString(address) { placemark, error in
if let placemark = placemark{
if let lattitude = placemark.first?.location?.coordinate.latitude, let longitude = placemark.first?.location?.coordinate.longitude{
lat = lattitude
long = longitude
print(lat)
}
}else if let err = error{
print(err.localizedDescription)
}
}
print(lat)
return CLLocationCoordinate2D(latitude: lat, longitude: long)
}
In this code when I print variable lat inside of geocoder it works, but when I try to print it outside it is 0.0. Why doesn't it work? Also 2nd print shows up earlier in console.