Anjana Sharma
Anjana Sharma

Reputation: 4745

setinterval or settimeout for google map v3

. I have 50 addresses coming from sql. Each address will be rendered inside a div LocationAddress. I loop through each of them, geocode and plot the marker in the map. But it only displays 11 of the. I know this is because of geocoder being bottlenecked. How do I use setinterval or settimeout to control this? I researched and saw people using settimeout to handle this..please someone guide me...

  var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(42.095287, -79.3185139);
        var myOptions = {
          maxZoom: 14,
          zoom: 9,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);

       }

    function codeAddress() {
        var infowindow = new google.maps.InfoWindow({}); 

        $('.LocationAddress').each(function() {

            var addy = $(this).text();
            geocoder.geocode( { 'address': addy}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);
                        var marker = new google.maps.Marker({
                        position: results[0].geometry.location,
                        map: map,               
                        title:addy,
                    });

                 //Adding a click event to the marker 
                google.maps.event.addListener(marker, 'click', function() { 
                    infowindow.setContent('<div id=\"infowindow\" style=" height:100px;>'
                                            +'<div id=\"LeftInfo\">'+ "Hello World!"
                                            +'</div>'+'</div>'); 
                    infowindow.open(map, this); 
                });  
             }  

            });//Geocoder END

        });
    }

Upvotes: 3

Views: 4216

Answers (2)

Mikko
Mikko

Reputation: 602

I actually coded it and tested, this one worked:

var geocoder;
var map;
var addresses = new Array();
var infowindow;
var theInterval;

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(42.095287, -79.3185139);
    var myOptions = {
        maxZoom: 14,
        zoom: 9,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    infowindow = new google.maps.InfoWindow({});
}

$(document).ready(function () {
    getAddresses();
    theInterval = setInterval("codeAddress()", 1000);
});

function getAddresses () {
    $('.LocationAddress').each(function () {
        addresses.push($(this).text());
    });
}

function codeAddress() {
    if (addresses.length == 0) {
        clearInterval(theInterval);
    }
    var addy = addresses.pop();
    geocoder.geocode({
        'address': addy
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map,
                title: addy,
            });

            //Adding a click event to the marker 
            google.maps.event.addListener(marker, 'click', function () {
                infowindow.setContent('<div id=\"infowindow\" style=" height:100px;>' + '<div id=\"LeftInfo\">' + "Hello World!" + '</div>' + '</div>');
                infowindow.open(map, this);
            });
        }

    }); //Geocoder END
}

Upvotes: 1

jfriend00
jfriend00

Reputation: 707456

You could serialize the requests so that you process one address at a time. When that one finishes, you process the next one. That could be done like this:

function codeAddress() {
    var infowindow = new google.maps.InfoWindow({}); 

    var addresses = $('.LocationAddress');
    var addressIndex = 0;

    function next() {
        if (addressIndex < addresses.length) {
            var addy = addresses.eq(addressIndex).text();
            ++addressIndex;
            geocoder.geocode( { 'address': addy}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);
                        var marker = new google.maps.Marker({
                        position: results[0].geometry.location,
                        map: map,               
                        title:addy,
                    });

                 //Adding a click event to the marker 
                google.maps.event.addListener(marker, 'click', function() { 
                    infowindow.setContent('<div id=\"infowindow\" style=" height:100px;>'
                                            +'<div id=\"LeftInfo\">'+ "Hello World!"
                                            +'</div>'+'</div>'); 
                    infowindow.open(map, this); 
                });  
                next();
             }  

            });//Geocoder END
        }
    }
    next();
}

Upvotes: 0

Related Questions