繁体   English   中英

在reverseGeocodeLocation(Swift2)中更改变量值

[英]Changing a variable value inside reverseGeocodeLocation (Swift2)

我在更改字符串变量值时遇到问题,代码看起来像这样

self.mapView是一个MKMapView

代码的作用:它抓住手指触摸点并在其中放置注释,并使用CLGeocoder()。reverseGeocodeLocation我想抓住此人的地址,但是当我尝试将地址存储在变量中时,它只会直接返回nilCLGeocoder()。reverseGeocodeLocation中使用注释 。标题确实会改变其值在这里有什么用?

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

打印输出(地址) 为零

我给你看我的榜样

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

发生这种情况是因为您要在块内设置address 完成获取反向位置后, CLGeocoder().reverseGeocodeLocation异步调用此块。 这就是为什么当您尝试立即在函数中打印它时,它是空的。 如果将其打印在块内,则会看到其具有正确的值。

您的函数可能如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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