CLGeocoder reverseGeocodeLocation是不同的纬度/经度的返回地标? (附带操场示例)
问题描述:
为什么CLGeocoder reverseGeocodeLocation在查找地址时返回具有不同纬度/经度的地标?CLGeocoder reverseGeocodeLocation是不同的纬度/经度的返回地标? (附带操场示例)
背景:在地图上用户“长按”放置一个引脚,但我的代码反向地理编码,这是为了能够在Pin上放置一个区域名称,但是然后丢弃的PIN是不同的位置(即不是一个好的用户体验)。所以我正在寻找一种方法来利用用户实际选择的lat/long,然后查找位置名称。
例如:采用下面的代码我看到:
- 输入:-28.7780218895614,152.978574011267
- OUTPUT:-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
答
这是一个正确的行为。地标将返回它的相关位置。这显然可能与输入的不同。当您为地理编码器提供位置时,该位置将“推断”您想要获取的地标。
假设这样的:
a: Associated location with the empire state building. b: Location you provided from an long press for instance. ------- | | | a | --> CLPlacemark: Empire state building, location: a | b| -------
你是对你的假设。 :)
PS。或者在这种情况下,采用由reverseGeocodeLocation返回的地标,并使用地点,子地点,国家字符串,但不是位置/坐标,而是使用“长按”的坐标? – Greg