Javascript Google maps, displays only 10 markers o

2020-04-17 08:02发布

问题:

Here is the link: http://alchemist3d.com/maptest.html

Also I'm using geocoder in a loop to get the coordinates of an array of addresses, here is the code:

function initialize() {
  var list = [
  {location:"residencial punta del sol casa 6  temixco Morelos Mexico",body : " 1", title : "m 1"},
  {location:"prol. harris num. 23 ampl. bugambilias jiutepec Morelos Mexico",body : "ampl. bugambilias 2", title : "f 2"},
  {location:"Gladiola Satelite Cuernavaca Morelos Mexico",body:"Montes de Oca"}
  ];
  var latlng = new google.maps.LatLng(18.92009,-99.20611);
  var myOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  //map.fitBounds(getBounds());
  codeLocations(list, map);
}

function codeLocations(list, map) {
  for (var i = 0; i < list.length; i++) {
    //console.log("Looping " + list[i].location);
    var geocoder = new google.maps.Geocoder();
    var geoOptions = {
      address: list[i].location,
      bounds: getBounds(),
      region: "NO"
    };
    geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map));
  }
}

function createGeocodeCallback(item, map) {
  //console.log("Generating geocode callback for " + item.location);
  return function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      //console.log("Geocoding " + item.location + " OK");
      addMarker(map, item, results[0].geometry.location);
    } else {
      //console.log("Geocode failed " + status);
    }
  }
}

function addMarker(map, item, location) {
  //console.log("Setting marker for " + item.location + " (location: " + location + ")");
  var marker = new google.maps.Marker({ map : map, position : location});
  marker.setTitle(item.title);
  var infowindow = new google.maps.InfoWindow( {
    content : item.body,
    size : new google.maps.Size(100, 300)
  });
  new google.maps.event.addListener(marker, "click", function() {
    infowindow.open(map, marker);
  });
}

function getBounds() {
  var myOptions = {
    zoom: 23,
    center: new google.maps.LatLng(-33.9, 151.2),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var southwest = new google.maps.LatLng(17.920,-100.206);
  var northeast =new google.maps.LatLng(19.920,-104.206);
  return new google.maps.LatLngBounds(southwest, northeast);
}

回答1:

Yes, there is a rate limit, which isn't too well documented. 9-10 requests per second? If you check the error status, it's called OVER_QUERY_LIMIT. With 100 markers, I don't think the common waiting technique will do you much good, because you have a lot of markers. It's best to geocode once, save the LatLngs and use them.

If you're brave, you can try:

setTimeout(function(){
  geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map));
}, 200*i);

I don't know why everyone uses 200 ms, maybe from trial and error? You can try 150, or 100. Still, at 100 ms it will take you 10 seconds to load 100 markers. Another idea I read about is to query in batches. Geocode ten, wait, then another ten.

An alternate function you can try is sleep.

function sleep(milliSeconds) {
  var startTime = new Date().getTime();
  while (new Date().getTime() < startTime + milliSeconds);
}


回答2:

Lilina is right about the rate limit, but it's possible to vary the rate depending on whether you fall foul of it or not. 200ms is likely to OK all the time, but see also this linked question where there's an example of a varying rate which starts faster than that (and is actually only slowed down to around 150ms).

The problem recounted there is exactly the same as yours.

Google Map V3 : Only some markers are displayed