【问题标题】:Google maps: Reverse geo code call back is not coming谷歌地图:反向地理编码回调不会到来
【发布时间】:2017-12-10 17:46:22
【问题描述】:

我正在尝试反转所有照片的地理编码位置。我正在使用反向地理编码方法如下。

func fetchLocation(coordinate: CLLocationCoordinate2D, completion: @escaping (_ location: LocationData?) -> Void) {

        let lockQueue = DispatchQueue(label:"com.LockQueue")
lockQueue.sync{

        DispatchQueue.main.async {
                        self.geocoder.reverseGeocodeCoordinate((coordinate), completionHandler: {
                            reverseGeoCodeResponse, _ in
                            if let reverseGeoCodeResponse = reverseGeoCodeResponse, let reverseGeoCode = reverseGeoCodeResponse.firstResult() {
     let locationData = LocationData()
                            locationData.locality = reverseGeoCode.locality
                            locationData.subLocality = reverseGeoCode.subLocality
                            locationData.administrativeArea = reverseGeoCode.administrativeArea
                            locationData.postalCode = reverseGeoCode.postalCode
                            locationData.country = reverseGeoCode.country
                            locationData.latitude = (coordinate.latitude)
                            locationData.longitude = (coordinate.longitude)
                            locationData.isDataFromGoogle = true
                                completion(locationData)

    }
    }

}
}

有时此方法不会返回回调给我。我不明白为什么添加此方法的另一件事可以由多个线程同时调用,所以我使用锁队列来确保我不会遇到同步问题。

【问题讨论】:

  • 您如何确定没有调用完成处理程序?我看到您忽略了该方法可能产生的任何错误。
  • 我正在确认完成处理程序正在返回,因为我的断点正在到达这一点 .self.geocoder.reverseGeocodeCoordinate((coordinate), completionHandler: {. } 所以我确认了。如何处理此错误方法。

标签: ios swift google-maps apple-maps


【解决方案1】:

经过几天的调试,我终于解决了这个问题。

我正在将 GEOCoder 作为实例变量使用 as self.geoCoder 现在我改为关注

DispatchQueue.main.async {
// new Code
let geoCoder = GMSGeoCoder()
geoCoder.reverseGeocodeCoordinate((coordinate), completionHandler: {
                            reverseGeoCodeResponse, _ in
}
        // OLD code                self.geocoder.reverseGeocodeCoordinate((coordinate), completionHandler: {
                            reverseGeoCodeResponse, _ in
}
}

我更改为局部变量并为每个 georeverse 调用创建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2013-07-29
    • 1970-01-01
    • 2014-05-18
    相关资源
    最近更新 更多