Search code examples
google-mapsgoogle-maps-api-3google-maps-markersmarkerclusterer

How do I get MarkerClusterer to work here?


I am attempting to implement the MarkerClusterer library into my map and so far with no success. The following is my code so far and below that is the error that I am receiving in the JavaScript console:

In the initialize function in addition to creating the map, also shows how the markers are created:

  var a = [];
  var t =  {
    name:"name1",
    lat:parseFloat(123),
    lng:parseFloat(-123),
    address:"address1",
    link:"link1",
    site:""
  }
    a[0] = t;

  var t = {
    name:"name2",
    lat:parseFloat(234),
    lng:parseFloat(-234),
    address:"address2",
    link:"link2",
    site:""
  }
    a[1] = t;



for (var i = 0; i < a.length; i++) {
    var latlng = new google.maps.LatLng(a[i].lat,a[i].lng);
    map.addMarker(createMarker(a[i].name,latlng,a[i].address,a[i].link,a[i].site));
 };

Outside the initialize function:

      function createMarker(name,latlng,address,link,site) {
        var marker = new google.maps.Marker({position: latlng});
        a.push(marker);
        google.maps.event.addListener(marker, "click", function() {
          if (infowindow) infowindow.close();
          infowindow = new google.maps.InfoWindow({content:"<div class='buildingInfo'>" + name + "<br>" + "<a href='" + link + "'>" + address +"</a>" + "</div>"});
          infowindow.open(map, marker);
        });
        return marker;

The markers all are created fine and such, I'm just not sure I am implementing the MarkerClusterer code correctly.

The error I am getting:

Uncaught TypeError: Cannot read property 'push' of undefined

So my big question is: Why is a undefined? I put var a; outside all functions to make it global.


Solution

  • GOT IT.

    Since a is already being used elsewise, all I literally had to do was add a new array, markers[] to the code and push that.