Google Maps API: MarkerClusterer not clustering and how to update markers periodically

timer Asked: Jul 6th, 2013

Question description

I have a couple of problems with my map that I haven't been able to solve through Googling and looking at other people's questions. I'm not too familiar with Javascript and AJAX, so some solutions I have found I can't seem to implement into my particular system. Sorry if similar questions are out there, I just haven't had any luck implementing those solutions.

The first problem I'm having is that I want to get a simple MarkerClusterer system in so the markers will cluster. I've added the scripts to my system, but they just don't cluster. When I used the data.json of the examples, the clustering worked; but getting the data into a variable using AJAX and using that doesn't seem to do a thing.

The second problem is that the markers are being added on top of each other periodically, rather than being updated. I'm guessing that in the simplest case I'll want to clear the old markers before adding the new ones, but whenever I try to add a solution proposed by others, all my markers disappear. So if someone could help out in being specific with my system, that'd be awesome.

Here's my javascript:

    <script type="text/javascript" src=""></script>

    <script type="text/javascript">

      var script = '<script type="text/javascript" src="';

      if ('compiled') !== -1) {

        script += '_compiled';


      script += '.js"><' + '/script>';



    <script type="text/javascript">    


      function initialize() {

        var myLatlng = new google.maps.LatLng(35.0839, -106.6186);

        var mapDiv = document.getElementById('map-canvas');

        map = new google.maps.Map(mapDiv, {

          center: new google.maps.LatLng(35.0839, -106.6186),

          zoom: 16,

          mapTypeId: google.maps.MapTypeId.HYBRID


setTimeout(initializeMaps, 5000);

function initializeMaps() {

var ajax;

ajax=new XMLHttpRequest();



  if (ajax.readyState==4 && ajax.status==200)


var markers = ajax.responseText;

var markers = JSON.parse(markers);





function addMap(markers) {

var iconBase = '';

    var infowindow = new google.maps.InfoWindow(), marker, i;

    for (i = 0; i < markers.length; i++) {  

        var marker = new google.maps.Marker({

            position: new google.maps.LatLng(markers[i][1], markers[i][2]),

            map: map,

            icon: iconBase + markers[i][3]


        google.maps.event.addListener(marker, 'click', (function(marker, i) {

            return function() {


      , marker);


        })(marker, i));



setTimeout(initializeMaps, 5000);

var markerCluster = new MarkerClusterer(map, markers); 




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


Tutor Answer

(Top Tutor) Studypool Tutor
School: UC Berkeley
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors