简体   繁体   中英

Changing a variable value inside reverseGeocodeLocation (Swift2)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM