1

I am attempting to plot multiple addresses using google maps api. I am having an issue returning the location value from the geocoding service for anything more that 11 test addresses. The test addresses return valid latlng values individually. My provided sample code will run, but when address[11] is not commented out it will not.

var geocoder;
var map;
var counter = -1;
  var address = [];
  address [0] = {loc:'memphis tn', pop:5};
  address [1] = {loc:'125 ketay', pop:2};
  address [2] = {loc:'tenafly nj', pop:4};
  address [3] = {loc:'california', pop:3};
  address [4] = {loc:'kansas city', pop:1};
  address [5] = {loc:'lake erie', pop:1};
  address [6] = {loc:'chicago', pop:1};
  address [7] = {loc:'oregon', pop:1};
  address [8] = {loc:'idaho', pop:1};
  address [9] = {loc:'nevada', pop:1};
  address [10] = {loc:'georgia', pop:1};
  //address [11] = {loc:'Las Vegas', pop:1};

var k = 0;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 5,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


 for (k = 0; k < address.length; k++){
  geocoder.geocode( {'address': address[k].loc}, function(results) {
  counter ++;
      map.setCenter(results[0].geometry.location);

 var cir = new google.maps.Circle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      center: results[0].geometry.location,
      radius: address[counter].pop * 100000
  });

  });
}
}

google.maps.event.addDomListener(window, 'load', initialize);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Could it be that you are hitting the rate limit? https://developers.google.com/maps/articles/geocodestrat#quota-limits "In addition, the Web Service is rate-limited, so that requests that come in too quickly result in blocking." – MrUpsidown Dec 25 '13 at 23:02
  • possible duplicate of [Google map geocoding stops, while executing recursively](http://stackoverflow.com/questions/13619409/google-map-geocoding-stops-while-executing-recursively) You don't check the [status](https://developers.google.com/maps/documentation/javascript/reference#GeocoderStatus) in the callback function. – geocodezip Dec 26 '13 at 04:24

1 Answers1

0

It looks like Google is punishing you for having calls come in too fast. It must think you are spamming the API. I modified you're code example and added a 250 ms delay between google API calls and it works fine. You can play with how short of a delay will work.

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&libraries=places"></script>
<script>
var geocoder;
var map;
var counter = -1;
  var address = [];
  address [0] = {loc:'memphis tn', pop:5};
  address [1] = {loc:'125 ketay', pop:2};
  address [2] = {loc:'tenafly nj', pop:4};
  address [3] = {loc:'california', pop:3};
  address [4] = {loc:'kansas city', pop:1};
  address [5] = {loc:'lake erie', pop:1};
  address [6] = {loc:'chicago', pop:1};
  address [7] = {loc:'oregon', pop:1};
  address [8] = {loc:'idaho', pop:1};
  address [9] = {loc:'nevada', pop:1};
  address [10] = {loc:'georgia', pop:1};
  address [11] = {loc:'Las Vegas', pop:1};

var k = 0;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 5,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


 for (k = 0; k < address.length; k++){

 //alert( address[k].loc );
  geocoder.geocode( {'address': address[k].loc}, function(results) { setTimeout( function(){
  counter ++;
      map.setCenter(results[0].geometry.location);

 var cir = new google.maps.Circle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      center: results[0].geometry.location,
      radius: address[counter].pop * 100000
  });
  }, 250 )
  });
}
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
<body>
<div id='map-canvas' style='width: 700px; height: 600px'>
</div>
</body>
Andrew - OpenGeoCode
  • 2,299
  • 18
  • 15