mdd
mdd

Reputation: 51

Google Maps Click Event on Multiple Markers

Can someone please tell me why this isn't working? I have been trying for two days now to try and figure out why I can't click on my markers. The markers appear just fine, but I can't get any click events. I'm not getting errors or anything, just no response. Please and thank you. Even if you just guide me in the right direction I'm someone that loves to learn, I can't seem to figure out my mistakes even in the Google Maps documentation.

markersArray[];

function addMarkers () {
  var bounds = map.getBounds();
  for (var i = 0; i < spaces.length; i++) {  
    var markerLatLng = {lat: parseFloat(spaces[i].lat), lng: parseFloat(spaces[i].log)};
    if (bounds.contains(markerLatLng)){
        var spaceMarker = {
            path: 'M43.8,0H6.2C2.8,0,0,3.2,0,7.1v25c0,3.9,2.8,7.1,6.2,7.1h12.5L31.2,50V39.3h12.5c3.4,0,6.2-3.2,6.2-7.1v-25C50,3.2,47.2,0,43.8,0z',
            fillColor: 'Black',
            fillOpacity: 0.7,
            strokeColor: 'Dodgerblue',
            strokeWeight: 0,
            labelOrigin: new google.maps.Point(24,19)
          };
        var markerLabel =  "$" + spaces[i].hourlyrate ;
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(spaces[i].lat, spaces[i].log),
            map: map,
            icon: spaceMarker,
            id: spaces[i].id,
            zIndex: 100,
            label:{
            text: markerLabel,
            color: "#FFFFFF",
            fontSize: "23px",
            fontWeight: "bold",
            fontFamily: "Lato"
            }
          });
          google.maps.event.addListener(marker, 'click', addClickToMarker);
          markersArray.push(marker);
    }
  }
}

function addClickToMarker() {
  console.log("test");
}

Upvotes: 0

Views: 922

Answers (1)

geocodezip
geocodezip

Reputation: 161404

The third argument to google.maps.addListener must be a function pointer (function name with no arguments) or return a function that will be executed when the event fires. addClickToMarker(marker) doesn't return a function and isn't a function pointer, so it executes once, returns null, after which every time you click on a marker you execute the null function.

Upvotes: 2

Related Questions