0

I'm trying to run through a loop in Wordpress and for each item in the loop I need to geocode the address, then add the LatLng into an array. I think I have it coded right but it will not work. Here is the code:

    <script type="text/javascript">
var locs = []
  function initialize() {

var geo = new google.maps.Geocoder;
  var address = "<?php echo $arrayAddress; ?>";
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {              
    var myLatLng = results[0].geometry.location;
    locs.push(myLatLng);
} else {
    alert("Geocode was not successful for the following reason: " + status);
}
});
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

I've verified that the myLatLng variable does contain the lat and long like it should. I just can't get it to push it into the variable. Is this some simple syntax I've missed?

WilliamAlexander
  • 374
  • 2
  • 4
  • 20
  • 1
    What do you mean "can't get it to push"? Are you hitting that error condition? Did you forget to include the code that actually references the `locs` array? – Evan Davis Aug 06 '14 at 20:56
  • Is geo.geocode an asynchronous function? You may be trying to access the value pushed to locs before it has actually been pushed. – Derongan Aug 06 '14 at 21:04
  • Your code should work fine but I'm afraid you reached the limit; as far as I know, you can't send more than about ten request in the row so you would need to implement a pause :) – hex494D49 Aug 06 '14 at 21:48
  • You can see the solution in below link [http://stackoverflow.com/questions/24894023/google-maps-api-function-does-not-return-results-in-time?rq=1](http://stackoverflow.com/questions/24894023/google-maps-api-function-does-not-return-results-in-time?rq=1) – Hoang Thanh Tu Feb 26 '17 at 02:38

0 Answers0