[GIS] Leaflet pass geoJson parameter to onEachFeature method

geojsonleaflet

I created geoJson object as below:

var geoJson= L.geoJson(groupingLevelType.type, {
                style: style,
                onEachFeature: onEachFeature
                });

    function onEachFeature(feature, layer) {
                layer.on({
                    mouseover: highlightFeature,
                    mouseout: resetHighlight
                });

    function resetHighlight(e) {
                geojson.resetStyle(e.target);
            }

In resetHighlight method I want to reset style of geoJson.How I can pass geoJson as parameter to onEachFeature method.Or there there is any other way separate onEachFeature from creation of geoJson to be able to reset style?

Best Answer

Thanks to JavaScript scoping, you should be able to use your geoJson variable within your onEachFeature function without the need to "pass [it] as parameter".

Note that you may have a typo: geojson.resetStyle(e.target); should be geoJsonā€¦ (note the capital J) to match your variable name.

Once this typo is corrected, your code looks to work: http://jsfiddle.net/ve2huzxw/180/