Get Layer info when hover the map in Leaflet

Viewed 1208

I have added a Geojson layer to a base map in Leaflet. I used the following event to get the Map data as well as Layer data:

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

it gave me the following log:

enter image description here

I could not find anything regarding the Geojson layer when I hover it (Nothing in the target about the layer).

Does anyone has any solution for this?

2 Answers

Here is the code to get the Geojson layer information:

$.ajax({
    dataType: "json",....

       onEachFeature: function (feature, layer) {
           layer.on('mousemove', function (event) {
                console.log(event);
           });
       }
});

You can use your map variable to get an info about the layers etc.

Actually when you create your layer it seems that it is stored also in a variable so that you can access it.

var map = L.map(...);
var geoLayer = L.geoJSON().addTo(map);
geoLayer.addData(geojsonFeature);

map.on('mousemove', function(event) {
  console.log(event, geoLayer, map);
});
Related