【问题标题】:Get Layer info when hover the map in Leaflet在 Leaflet 中悬停地图时获取图层信息
【发布时间】:2019-01-14 16:04:52
【问题描述】:

我已将 Geojson 图层添加到 Leaflet 中的底图。我使用以下事件来获取地图数据以及图层数据:

map.on('mousemove', function (e) {
    console.log(e);
});

它给了我以下日志:

当我将其悬停时,我找不到任何关于 Geojson 图层的信息(target 中没有关于该图层的任何内容。

有没有人可以解决这个问题?

【问题讨论】:

    标签: javascript leaflet gis


    【解决方案1】:

    获取Geojson层信息的代码如下:

    $.ajax({
        dataType: "json",....
    
           onEachFeature: function (feature, layer) {
               layer.on('mousemove', function (event) {
                    console.log(event);
               });
           }
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用 map 变量来获取有关图层等的信息。

      实际上,当您创建图层时,它似乎也存储在一个变量中,以便您可以访问它。

      var map = L.map(...);
      var geoLayer = L.geoJSON().addTo(map);
      geoLayer.addData(geojsonFeature);
      
      map.on('mousemove', function(event) {
        console.log(event, geoLayer, map);
      });
      

      【讨论】:

      • 我需要鼠标悬停的特征信息。这段代码只是为我们提供了存在于 geoLayer 中的全部特征数据。
      • 那么你需要在每个对象上使用鼠标处理程序而不是地图本身
      • 正确,我在onEachFeature 选项中使用了layer.on('mousemove', function (event)...
      猜你喜欢
      • 1970-01-01
      • 2021-01-20
      • 2018-01-29
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      相关资源
      最近更新 更多