15

我的应用程序中有一个地址字段,用户需要在其中输入所需的地址。我使用 google Geocoder 来获取地址的 GPS 坐标。但现在我想通过使用 Places Autocomplete 让用户更轻松。但是 Places Autocomplete 只返回地址、Id 和地址的引用。

有没有办法使用 Places Autocomplete API 获取用户选择的地址的 GPS 坐标?用户从 Places Autocomplete 中选择他的地址后,我是否必须再次使用 Geocoder?

或者我应该在用户选择他的地址后再次使用 Places API 来获取地址的 GPS 坐标吗?由于使用限制,我不想向 Places API 发送多个请求。. 这是 Places API 的预期响应。响应中没有 GPS 坐标。

"status": "OK",
  "predictions": [ {
    "description": "Paris, France",
    "id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
    "reference": "CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "France",
      "offset": 7
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }, {
    "description": "Paris, TX, United States",
    "id" : "518e47f3d7f39277eb3bc895cb84419c2b43b5ac",
    "reference": "CjQjAAAAHnbxZZ...BDR3iIOFdMTxwo1jHg",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "TX",
      "offset": 7
    }, {
      "value": "United States",
      "offset": 11
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }, {
    "description": "Paris, Ontario, Canada",
    "id" : "e7ac9c89d4a590305242b0cb5bf43064027223c9",
    "reference": "CjQhAAAAIv_YWYt...F8KZHY36TwMrbyu_g",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "Ontario",
      "offset": 7
    }, {
      "value": "Canada",
      "offset": 16
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }

4

1 回答 1

14

您只需使用 Places API 的Place Details部分,使用“reference”值从每个建议中的引用中获取实际位置。例如:

https://maps.googleapis.com/maps/api/place/details/json?reference=CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc&sensor=true&key=AddYourOwnKeyHere

这将为您提供有关该地点的信息,包括其坐标(特定位置的点,区域的边界)。

于 2013-02-21T17:23:20.740 回答