Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a popup and hover with leaflet js plugin?

I'm using the drupal leaflet module and want to have a popup on click, and then a mouseover to show up in the corner when hovering. Currently I have the popup working but cannot add a mouseover. Everywhere I"ve read is saying that you can add a mouseover to the feature wih the geoJson object but it doesn't look like I have access to that object using it through this module. Here is my Js code.

(function ($) {
Drupal.behaviors.maps = {
attach:function (context, settings) {

  // Add legends to each leaflet map instance in Drupal's settings array
  $(settings.leaflet).each(function() {
    // Get the map object from the current iteration
    var map = this.lMap;

    // Create a legend class that will later be instantiated and added to the map
    var legend = L.Control.extend({
      options: {
        position: 'bottomleft'
      },

      onAdd: function (map) {
        // create the control container div with classes
        var container = L.DomUtil.create('div', 'info legend');

        var html = '<h1>Status</h1>';
        html += '<ul>';
        html += ' <li><span class="color home"></span> Available Home</li>';
        html += ' <li><span class="color lot"></span> Available Lot</li>';
        html += ' <li><span class="color not-available"></span> Not Available</li>';
        html += '</ul>';
        container.innerHTML = html;

        return container;
      }
    });
    map.scrollWheelZoom.disable();
    map.addControl(new legend());
  });
}
};
})(jQuery);

I have the popup working, I need to add an on hover to each feature, how can I do that?

like image 994
jakecraige Avatar asked Jul 23 '13 16:07

jakecraige


People also ask

How do you show pop up in leaflet?

If you need to show the popup for a marker you can use markers bindPopup method. Then you have more control and it will automatically be bound to your marker. In the example below you can show the popup when the user mouses over, and hide it when the user mouses out: marker.

How do you add a leaflet to a plugin?

You can attach the Leaflet plugin to your map with the help of Github or any other repository via adding the additional . js and . css file into the leaflet html file in the header section. The leaflet plugins really improves the performance and usability of Leaflet Js.

What is Leaflet marker?

Advertisements. To mark a single location on the map, leaflet provides markers. These markers use a standard symbol and these symbols can be customized. In this chapter, we will see how to add markers and how to customize, animate, and remove them.


1 Answers

When you create your geojson layer, you can pass functions:

var myLayer = L.geoJson(d, {style: style, onEachFeature: onEachFeature, pointToLayer: pointToLayer}).addTo(map);

onEachFeature specifies what functions are to be called depending on the event:

var onEachFeature = function onEachFeature(feature, layer) {
        layer.on({
            mouseover: highlightFeature,
            mouseout: resetHighlight,
            click: zoomToFeature,
            pointToLayer: pointToLayer
        });
    };

Example of mouseover function:

function highlightFeature(e) {
    var layer = e.target;

    layer.setStyle({ // highlight the feature
        weight: 5,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.6
    });

    if (!L.Browser.ie && !L.Browser.opera) {
        layer.bringToFront();
    }
    map.info.update(layer.feature.properties); // Update infobox
}

You need to modify the above code to fit your design, but it shows you how you can get the hover on each feature working.

like image 131
Anna Pawlicka Avatar answered Oct 12 '22 12:10

Anna Pawlicka