2014-12-30 44 views
5

我用網址:Google Places Autocomplete - 如何獲取經度和緯度?

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=bar&key=API_KEY&types=geocode 

我應該怎麼做來檢索與地方的位置(經度和緯度)的數據?

+0

必須生成在谷歌地圖開發API_KEY。然後在你的url中傳遞API_KEY。你必須使用(https://developers.google.com/maps/signup) –

+0

註冊並創建密鑰,我知道我通過了它。有用。我收到來自Google的地點,但沒有關於地點的信息。 –

回答

17

不可能只有這樣網址:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=bar&key=API_KEY 

所有我需要做的是從響應得到place_id,然後在下次使用它下面的網址:

https://maps.googleapis.com/maps/api/place/details/json?input=bar&placeid=PLACE_ID&key=API_KEY 

其中:

PLACE_ID - 從以前的請求中檢索到place_id

API_KEY - 我的密鑰由谷歌產生,用於我的應用程序。

autocomplete必須在上面的URL中用details代替。

+0

欣賞你的答案隊友:) – Xeieshan

+0

結果[0]我收到的是lat:function()&lng function(),我沒有得到這些值。 – jasan

+0

算出來了,函數返回值:) – jasan

0
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=bar&key=API_KEY 

然後得到place_id和呼叫下面的函數來獲取數據

let placesClient = GMSPlacesClient.shared() 
    func GetPlaceDataByPlaceID(pPlaceID: String) 
    { 
     // pPlaceID = "ChIJXbmAjccVrjsRlf31U1ZGpDM" 
     self.placesClient.lookUpPlaceID(pPlaceID, callback: { (place, error) -> Void in 

      if let error = error { 
       print("lookup place id query error: \(error.localizedDescription)") 
       return 
      } 

      if let place = place { 
       print("Place name \(place.name)") 
       print("Place address \(place.formattedAddress!)") 
       print("Place placeID \(place.placeID)") 
       print("Place attributions \(place.attributions)") 
       print("\(place.coordinate.latitude)") 
       print("\(place.coordinate.longitude)") 
      } else { 
       print("No place details for \(pPlaceID)") 
      } 
     }) 
    }