0

I've added a google map which I feed thanks to the Geofield module.

I'd like to add an onClick event to each marker to have a custom behavior.

this is what I've done this far :

  Drupal.behaviors.geofieldGoogleMapInteraction = {
    attach: function (context, settings) {
      if (context !== document) {
        return;
      }

      // React on geofieldMapInit event.
      $(document).on('geofieldMapInit', function (e, mapid) {
        var map = Drupal.geoFieldMapFormatter.map_data[mapid].map;
        var markers = Drupal.geoFieldMapFormatter.map_data[mapid].markers;
        map.addMarkerAddedCallback(function(markerClicked) { // is not a function
          triggerClick(markerClicked);
        });

        function triggerClick(markerClicked) {
          console.log("click !")
        }
        $.each(markers, function (storeId, marker) {
          if(parseInt(storeId) !== 56) return;
          const properties = marker.geojsonProperties;
          $(marker).on('click', function() {
            console.log("click") // not triggered
          });
        })
      });
    }
  };

I can't manage to trigger the on click of the marker ,I've got nothing in my console. Where am I wrong ?

EDIT : this is the source code of a single marker. I don't see any sign of how the marker id could be retrieved... (I have cluster and single markers at the same time)

enter image description here

1 Answer 1

4

If it help someone, this is what I came up with :

  $(document).on('geofieldMapInit', function (e, mapid) {
    var map = Drupal.geoFieldMapFormatter.map_data[mapid].map;
    var markers = Drupal.geoFieldMapFormatter.map_data[mapid].markers;

    $.each(markers, function (storeId, marker) {
      const properties = marker.geojsonProperties;
      google.maps.event.addListener(marker, 'click', function() {
        console.log('click ' + storeId)
      })
    })
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.