2017-02-27 25 views
4

爲什麼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 
+0

PS。或者在這種情況下,採用由reverseGeocodeLocation返回的地標,並使用地點,子地點,國家字符串,但不是位置/座標,而是使用「長按」的座標? – Greg

回答

1

這是一個正確的行爲。地標將返回它的相關位置。這顯然可能與輸入的不同。當您爲地理編碼器提供位置時,該位置將「推斷」您想要獲取的地標。

假設這樣的:

 
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| 
    ------- 

你是對你的假設。 :)