Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Google Maps API V3 throttle queries

I have been trying to throttle google maps api query over so that it will return more than 10 queries all morning with no luck. I have used this code here http://pastebin.com/X0icm1B8. I tried wrapping .each in a setTimeOut function at line 44 as well as trying it with the marker var but not luck. I found several results in google but haven't been able to get any of them to work in my code. Any help would be great thanks so much in advance.

2 Answers

Jeremy Germenis
Jeremy Germenis
29,854 Points

You need to grab all the addresses and sending them over in an array in one shot instead of one by one.

Here is how I figured it out. With help from this post http://stackoverflow.com/questions/9796730/setinterval-or-settimeout-for-google-map-v3?codekitCB=410851502.832954 I created a new array for the addresses with a function. Then place the existing address query inside a function with some changes. This allowed me to instead of setTimeout use setInterval. Worked perfect. Below is the code if anyone else needs it.

var map,
bounds,
geocoder,
addresses = new Array(),
center;

function addMarkerToMap(location, address){
//var image = "assets/img/set_image.png"; // Replace set_image.png with image path 
var marker = new google.maps.Marker({
map: map,
position: location,
//icon: image
});

bounds.extend(location);
map.fitBounds(bounds);

var infoWindow = new google.maps.InfoWindow({
content: address
});

google.maps.event.addListener(marker, 'click', function() {
infoWindow.open(map, marker);
});
}

function initialize() {
var mapOptions = {
scrollwheel: false,
mapTypeControl: false,
streetViewControl: false,
zoom: 10,
center: new google.maps.LatLng(37.09024, -95.712891),
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
geocoder = new google.maps.Geocoder();
bounds = new google.maps.LatLngBounds();
}

initialize();

// Created the array 
function getAddresses () {
$('address',parent.window.document).each(function () {
addresses.push($(this).html());
});
}

getAddresses();
theInterval = setInterval('codeAddress()', 100); // Set the Interval

// New Function for address
function codeAddress() { 
if (addresses.length == 0) {
clearInterval(theInterval);
}

var address = addresses.pop();

geocoder.geocode({ address: address }, function( results, status ) {
if(status == google.maps.GeocoderStatus.OK) addMarkerToMap(
results[0].geometry.location, address );
});
}

google.maps.event.addDomListener(map, 'idle', function() {
center = map.getCenter();
});

$(window).resize(function() {
map.setCenter(center);
});