【问题标题】:How to get geocode from json data?如何从 json 数据中获取地理编码?
【发布时间】:2021-03-31 10:43:35
【问题描述】:

我在地址变量中有这样格式的 json 数据:[{"address":"56 rue de la liberte 33000 bordeaux"},{"address":"38 rue de Paris 92000 Paris"},{"address":"12 rue du petit chat noir 47300 villeneuve sur lot"}]

我有我的地理编码功能:

var addresses = json

function geocodeAddress(location) {
geocoder.geocode({ address: location }, function (results, status) {
  //alert(status);
  if (status == google.maps.GeocoderStatus.OK) {
    //alert(results[0].geometry.location);
    console.log(results);
  } else {
    alert("some problem in geocode" + status);
  }
});

}

如何将我的地址数据传递给地理编码功能? 我做了geocodeAddress(addresses),但它不起作用。 如何将 json 数据作为参数传递给该函数?

谢谢

【问题讨论】:

  • 你不能。您需要遍历这些值并对每个地址分别进行地理编码。服务以rate limits为准。

标签: google-maps google-geocoder


【解决方案1】:

好的,我知道了,谢谢你:

for (i = 0; i < addresses.length; i++) {
geocoder.geocode(
  { address: addresses[i].address },
  function (results, status) {
    //alert(status);
    if (status == google.maps.GeocoderStatus.OK) {
      //alert(results[0].geometry.location);
      console.log(results);
    } else {
      alert("some problem in geocode" + status);
    }
  }
);

}

【讨论】:

  • 我想如果您有多个地址(超过 50 个),这不会起作用,因为如果您不使用一些超时,您可能会达到 API 速率限制- 正如我在评论中解释的那样。如果您想提供自己的答案,至少提供一个可行的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多