将反向地理编码的结果分配给全局变量

Allocating the results of Reverse Geocoding to a global variable

我正在使用 Swift 4 和 Xcode 9.3.1。我包括代码的屏幕截图。

总的来说,我是移动开发/编程的新手,一直在思考如何表达这一点。所以这是我能解释的最好的:

我正在构建一个获取用户位置的应用程序,以便向他们发送帮助。该应用程序获取用户的坐标,并显示带有地址信息的 TextView。非常直接的 mapKit/coreLocation 功能。到目前为止,一切顺利:使用 startUpdatingLocation() 获取坐标工作正常,我使用反向地理编码器获取 街道名称和地点 。但是 它们——意思是解码后的街道和地点字符串——只有在我在闭包 内调用它们时才会打印出来,而不是在闭包外调用它们。我理解 (正确或错误?) class 中的多个函数需要可用的变量应该在顶部全局声明。但是我不知道如何从此闭包中提取信息以便在其他地方使用它。

我一直在谷歌搜索和阅读 Whosebug 中的问题,我觉得缺少一些非常简单的东西,但无法弄清楚是什么。到目前为止我尝试失败的事情:

1_class开头定义全局变量为空字符串 并在发生地理编码反向方法的闭包内使用变量名称,以尝试存储结果字符串,但是当我尝试在闭包外打印变量时,全局变量仍然是空字符串(“”)。

[顶部声明的全局变量][1]

2_Defining 一个空的全局 字符串数组 并将闭包内部的信息附加到全局数组。在闭包外仍然有一个空数组。 (与 1 相同)

3_Create 函数 --func decodedString()-- 将 return 数据作为字符串,因此我可以通过声明

*let streetLocation : String = decodedString()*

然而,当我这样声明该函数时:

var street = ""
var locality = ""

// Get the street address from the coordinates
    func deocodedString() -> String {
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(location) { placemarks, error in
        if let placemark = placemarks?.first {
        self.street = placemark.name!
        self.locality = placemark.locality!
        let string = "\(self.street), \(self.locality)"
        return string
        }
    }
}

我得到一个错误:unexpected non-void return value in void function

unexpected no void return value in void function

最后,如果我使用下面的代码将信息直接传递到闭包内的 TextView,我的 textView 会成功更新——但我无法格式化字符串,我需要这样做才能制作它们看起来像我遵循的设计说明(也就是一些粗体文本、一些常规文本和一些不同大小的文本):

CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
    if let placemark = placemarks?.first {
        self.street = placemark.name!
        self.locality = placemark.locality!
        let string = "\(self.street), \(self.locality)"
        self.addressTextView.text = string
    }
}

所以这就是为什么我不能用 textView.text = string 方法传递它。

非常感谢您的帮助...我一直在查看 Whosebug、youtube 和其他教程位置,但我无法弄清楚我遗漏了什么,或者为什么我的函数声明会生成错误。在过去的 24 小时内,我已经多次销毁和反转我的代码,但没有得到一个独立的字符串,我可以在将它传递到 textView 之前应用格式,我不知道如何处理它。

当您调用此函数时,reverseGeocodeLocation 在后台线程中运行。所以如果你想 return 这个方法中的地址你应该使用 escaping closure.

    func getaddress(_ position:CLLocationCoordinate2D,completion:@escaping (String)->()) {
     let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(location) { placemarks, error in
        if let placemark = placemarks?.first {
        let street = placemark.name!
        let locality = placemark.locality!
        let string = "\(street), \(locality)"
        completion(string)
        }
    } 
    }

   self.getaddress(position.target) { address in
        print(address)
        self.addressTextView.text = address    
   }

我在使用 google 地理编码器更新地图屏幕上的标签时遇到问题。

所以我这样做了,首先,创建

swift文件名:GoogleAPI随便叫就可以了

class GoogleAPI {

    static let sharedInstance = GoogleAPI()

    private init() { }

    var addressValue = ""

    public func geocoding(lat: Double, long: Double) {
        Alamofire.request("https://maps.googleapis.com/maps/api/geocode/json?latlng=\(lat),\(long)&key=YOUR_GOOGLE_KEY").responseJSON { (response) in
            if response.result.isSuccess {
                let dataJSON : JSON = JSON(response.result.value!)
                self.geocoding(json: dataJSON)
            } else {
                print("Error \(response.result.error!)")
            }
        }
    }

    fileprivate func geocoding(json: JSON) {
        let json = json["results"]
        let address = json[1]["formatted_address"].stringValue
        addressValue = address
        print("pin address \(addressValue)")
    }
}

这是对 Google 的 API 调用,用于从响应中获取所有内容并解析唯一的街道。

然后使用地图转到您的视图控制器,其中有图钉、地图等。

设置可拖动的图钉、标记。 [marker1.isDraggable = true]

然后添加这个功能

mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker)

并像这样从上面添加调用:

func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
    GoogleAPI.sharedInstance.geocoding(lat: marker.position.latitude, long: marker.position.longitude)
    DispatchQueue.main.async {
        self.txtSearch.text = GoogleAPI.sharedInstance.addressValue
    }
}

txtSearch 是我的搜索文本字段。

是的,我知道,可以做得更好,但没时间了。这是工作。

Swift 4.2