Score:0

Geofield map : add marker on click listener

us flag

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

Score:2
us flag

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)
      })
    })
  });
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.