-2

I have json data formated like that in an addresses variable : [{"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"}]

and i have my geocode function :

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);
  }
});

}

how can i pass my address data to the geocode function ? I did geocodeAddress(addresses) but it dooesnt't work. How can i pass json data as an argument to this function ?

Thanks

  • 2
    You can't. You need to loop through the values and geocode each address separately. The service is subject to [rate limits](https://developers.google.com/maps/documentation/javascript/geocoding#rate-limits). – MrUpsidown Mar 31 '21 at 11:01

1 Answers1

0

Okay i found out thank you anyway :

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);
    }
  }
);

}

  • I suppose this is **not** going to work if you have several addresses (more than 50) as you will likely reach the API rate limits if you don't use some timeout - just as I explained in my comment. If you want to provide your own answer, at least provide a working one. – MrUpsidown Mar 31 '21 at 11:33