【问题标题】:Reverse geo coding return only one address when from Arrays of latitude , longitude从纬度、经度数组中反向地理编码仅返回一个地址
【发布时间】:2019-10-28 06:41:38
【问题描述】:

我有一个 CLLocationCoordinates2D 数组并设置了一个 for 循环来调用函数 get_addres_from_geoCoder 但是这个函数只打印一个地址,而 CLLocationCoordinates2D 数组有 18 个值(纬度,经度)

override func viewDidLoad() {
  super.viewDidLoad()

  for a in My_rides_Pick_locatio
  {
     let loc =       CLLocationCoordinate2D(latitude:a.latitude,longitude: a.longitude)

     self.get_addres_from_geoCoder(loc:  loc)
  }
func get_addres_from_geoCoder(loc : CLLocationCoordinate2D) {


            let location: CLLocation =  CLLocation(latitude: loc.latitude, longitude: loc.longitude)

            geo.reverseGeocodeLocation(location as CLLocation, preferredLocale: nil) { (clPlacemark: [CLPlacemark]?, error: Error?) in
                guard let place = clPlacemark?.first else {
                    print("No placemark from Apple: \(String(describing: error))")
                    return
                }

                    print("----")
                    print(place.compactAddress!)

            }
    }
}

结果


R A Bazaar 路,拉瓦尔品第,巴基斯坦

【问题讨论】:

    标签: ios arrays swift google-maps reverse-geocoding


    【解决方案1】:

    这是 Apple 的限制。请参阅文档。

    讨论 该方法将指定的位置数据异步提交给地理编码服务器并返回。当请求完成时,地理编码器在主线程上执行提供的完成处理程序。

    发起反向地理编码请求后,不要尝试发起另一个反向或正向地理编码请求。每个应用程序的地理编码请求都有速率限制,因此在短时间内发出过多请求可能会导致某些请求失败。当超过最大速率时,地理编码器会将值为 CLError.Code.network 的错误对象传递给您的完成处理程序。

    【讨论】:

      【解决方案2】:

      Chris 在上一个答案中是正确的,您不应该使用大量请求向 Apple 地理编码服务“发送垃圾邮件”,因为有些请求可能会被丢弃。但是,在这种情况下,我不确定这正是正在发生的事情,就好像每一个失败都会触发你的保护子句(因为即使对于根据文档删除的请求,仍然会调用完成处理程序)并且你会打印 17 次错误消息。

      如果某个请求已经处于活动状态,goecoding 服务似乎甚至不会接受请求。我刚刚完成的一些简短测试(因为它与我的一个项目相关,并且我想确保我理解它)似乎证实了这一点,因为它只会返回对提交的第一个坐标的响应,并且永远不会返回错误为后续的。

      因此,在您的场景中,更好的方法是使用迭代方法,在完成处理程序中启动剩余位置的反向地理编码:

      let geo = CLGeocoder()
      
      func reverse(locs: [CLLocation], number: Int = 1){
         guard !locs.isEmpty else {
            print("All Done")
            return
         }
         geo.reverseGeocodeLocation(locs.first!){ placemarks, error in
            print("-------------- \(number) -------------")
            if let error = error {
               print("Error \(error)")
            } else if let placemark = placemarks?.first {
               print("place: \(placemark)")
            }
            reverse(locs: Array(locs.dropFirst()), number: number + 1)
         }
      }
      

      number 参数和 prints 语句就在其中,因此您可以轻松查看正在发生的事情,并且需要从任何最终代码库中删除。

      作为如何使用它的简单演示,下面有一个示例。我已经在测试中成功处理了包含 20 个位置的数组。

      let c1 = CLLocation(latitude: 51.5074, longitude: 0.1278)
      let c2 = CLLocation(latitude: 52.5074, longitude: -0.1278)
      let c3 = CLLocation(latitude: 53.5074, longitude: -0.1378)
      let c4 = CLLocation(latitude: 54.5074, longitude: -0.1578)
      let c5 = CLLocation(latitude: 55.5074, longitude: -0.1778)
      
      let c = [c1, c2, c3, c4, c5]
      
      reverse(locs: c)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多