CLGeocoder的reverseGeocodeLocation方法返回的地标与经纬度不一致?(附带playground示例)

5
CLGeocoder的reverseGeocodeLocation方法在查找地址时,为什么返回的地标的经纬度与输入的经纬度不同?
背景:当用户长按地图以落下一个大头针后,我的代码需要进行反向地理编码来获取该大头针所在的区域名称,但实际上大头针落在了不同的位置(用户体验较差)。因此,我正在寻找一种方法,使得代码能够利用用户选择的经纬度,并且只查找其所在位置的名称。
例如:使用下面的代码,我看到:
输入:-28.7780218895614,152.978574011267 输出:-28.864405,153.0001191
代码:
import UIKit
import CoreLocation
import PlaygroundSupport

// Initial Test Co-ordinate
let locInput = CLLocation(latitude: -28.778021889561444, longitude: 152.97857401126666)
print("Input: \(locInput.coordinate.latitude), \(locInput.coordinate.longitude)")

// Initiate Reverse Geocoding
let geocoder: CLGeocoder = CLGeocoder()
print("About to call reverse geocoding function")
geocoder.reverseGeocodeLocation(locInput) { placemarks, error in
    guard let placemarks = placemarks else {fatalError("No Placemarks Provided \(error?.localizedDescription)")}
    for p in placemarks {
        print("OUTPUT:  \(p.location!.coordinate.latitude), \(p.location!.coordinate.longitude)")
    }
}

// Enable Asynch
PlaygroundPage.current.needsIndefiniteExecution = true

PS. 在这种情况下的方法是采用reverseGeocodeLocation返回的placemark,并使用locality、sublocality、country字符串,但不使用它的位置/坐标,而是使用“长按”的坐标吗? - Greg
1个回答

2

这是正确的行为。Placemark将返回其关联位置。这显然可能与输入的位置不同。当您向地理编码器提供位置时,它将“推断”您想要获取的Placemark。

假设如下:

 a:帝国大厦的关联位置。
 b:您通过长按等方式提供的位置。
------- | | | a | --> CLPlacemark:帝国大厦,位置:a | b| -------

您的假设是正确的。 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接