【问题标题】:How to do a popup and hover with leaflet js plugin?如何使用传单 js 插件弹出和悬停?
【发布时间】:2013-07-23 16:02:02
【问题描述】:

我正在使用 drupal 传单模块,并希望在单击时弹出一个弹出窗口,然后在悬停时将鼠标悬停在角落中。目前我的弹出窗口工作但无法添加鼠标悬停。我读过的所有地方都说您可以使用 geoJson 对象向功能添加鼠标悬停,但看起来我无法通过此模块使用它访问该对象。这是我的 Js 代码。

(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);

我的弹出窗口正常工作,我需要为每个功能添加悬停,我该怎么做?

【问题讨论】:

    标签: javascript drupal leaflet


    【解决方案1】:

    当你创建你的 geojson 层时,你可以传递函数:

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

    onEachFeature 指定根据事件调用哪些函数:

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

    鼠标悬停功能示例:

    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
    }
    

    您需要修改上述代码以适合您的设计,但它会向您展示如何使悬停在每个功能上正常工作。

    【讨论】:

    • 这个问题是我没有手动创建geoJson层。使用 Drupal 模块,我将数据传递给“leaflet_render_map”函数,因此无需直接编辑传单模块,我无法直接访问 geoJson 对象
    猜你喜欢
    • 2022-12-19
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    相关资源
    最近更新 更多