Reputation: 3
I am having an issue changing a string variable value the code looks like this
self.mapView is an MKMapView
what the code does: It grabs the finger touch point and put an annotation in its place and by using CLGeocoder().reverseGeocodeLocation I want to grab the person Address but when I try to store the address in a variable it just returns nil meanwhile directly using annotation.title inside CLGeocoder().reverseGeocodeLocation it does change its value what's the deal here?
func longPressAction(gestureRecognizer: UIGestureRecognizer) {
if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
let touchPoint = gestureRecognizer.locationInView(self.mapView);
let annotation = MKPointAnnotation();
let newCoords = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView);
let location = CLLocation(latitude: newCoords.latitude, longitude: newCoords.longitude);
var address:String = ""; // Doesn't store here!
CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) -> Void in
if (error != nil) {
} else {
if let p = placemarks?[0] {
if (p.subThoroughfare != nil) {
address = p.subThoroughfare!;
} else if (p.subThoroughfare == nil && p.thoroughfare != nil) {
address = p.thoroughfare!;
} else if (p.subThoroughfare == nil && p.thoroughfare == nil && p.country != nil) {
address = p.country!;
} else if (p.subThoroughfare == nil && p.thoroughfare == nil && p.country == nil) {
address = "New Place";
}
}
}
}
print(address)
annotation.title = address;
annotation.coordinate = newCoords;
self.mapView.addAnnotation(annotation);
sharedAnnotations.addObject(annotation);
}
Output of print(address) is nil
Upvotes: 0
Views: 101
Reputation: 6851
I show you my example
func returnPlaceMark(coordinate: CLLocationCoordinate2D, completaion: (superPlace: MKPlacemark) -> ()) {
let geocoder = CLGeocoder()
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
geocoder.reverseGeocodeLocation(location) { (arrayPlaceMark, error) -> Void in
if error == nil {
let firstPlacemark = arrayPlaceMark!.first!
completaion(superPlace: MKPlacemark(placemark: firstPlacemark))
}
}
Upvotes: 0
Reputation: 389
This happens because you're setting address
inside a block. This block is called asynchronously by CLGeocoder().reverseGeocodeLocation
when it finishes getting the reverse location. That's why when you try to print it right away in your function, it's empty. If you print it inside the block, you'll see it has the correct value.
Your function could look something like this:
func longPressAction(gestureRecognizer: UIGestureRecognizer) {
if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
let touchPoint = gestureRecognizer.locationInView(self.mapView)
let annotation = MKPointAnnotation()
let newCoords = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
let location = CLLocation(latitude: newCoords.latitude, longitude: newCoords.longitude)
CLGeocoder().reverseGeocodeLocation(location) { [weak self] (placemarks, error) -> Void in
if let strongSelf = self{
if (error != nil) {
} else {
if let p = placemarks?[0] {
var address:String = ""
if (p.subThoroughfare != nil) {
address = p.subThoroughfare!
} else if (p.subThoroughfare == nil && p.thoroughfare != nil) {
address = p.thoroughfare!
} else if (p.subThoroughfare == nil && p.thoroughfare == nil && p.country != nil) {
address = p.country!
} else if (p.subThoroughfare == nil && p.thoroughfare == nil && p.country == nil) {
address = "New Place"
}
print(address)
annotation.title = address
annotation.coordinate = newCoords
strongSelf.mapView.addAnnotation(annotation)
strongSelf.sharedAnnotations.addObject(annotation)
}
}
}
}
}
Upvotes: 1