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

FratBro23
Category:
Programming
Price: $5 USD

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="http://maps.google.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">

      var script = '<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer';

      if (document.location.search.indexOf('compiled') !== -1) {

        script += '_compiled';

      }

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

      document.write(script);

    </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();

ajax.onreadystatechange=function()

  {

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

    {

var markers = ajax.responseText;

var markers = JSON.parse(markers);

      addMap(markers); 

    }

  }

ajax.open("GET","ajax-getbubbles.php",true);

ajax.send();

function addMap(markers) {

var iconBase = 'http://mydomain.com/images/';

    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() {

                infowindow.setContent(markers[i][0]);

                infowindow.open(map, marker);

            }

        })(marker, i));

    }

}

setTimeout(initializeMaps, 5000);

var markerCluster = new MarkerClusterer(map, markers); 

}

     

      }

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

    </script>


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Duke University
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1830 tutors are online

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