CLGeocoder reverseGeocodeLocation返回带有不同纬度/经度的地标吗?(附有游乐场示例)

Gre*_*reg 5 mkmapview ios clgeocoder reversegeocodelocation

为什么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
Run Code Online (Sandbox Code Playgroud)

小智 2

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

假设这样:

a:与帝国大厦相关的位置。
 b:例如,您通过长按提供的位置。

    --------
    | |
    | 一个 | --> CLPlacemark:帝国大厦,位置:a
    | 乙|
    --------

你的假设是正确的。:)