简体   繁体   English

leaflet.js在单独列表的鼠标悬停时突出显示GeoJSON

[英]leaflet.js highlight GeoJSON on mouseover of separate list

edit: Here is a jsfiddle mockup: https://jsfiddle.net/6t8gnegf/ 编辑:这是一个jsfiddle样机: https ://jsfiddle.net/6t8gnegf/

I have a table of locations outside the map and I want to have each area highlight when the corresponding table element is moused over. 我在地图外有一张位置表,当鼠标悬停在相应的表元素上时,我希望每个区域都突出显示。 So I first make a list of the geojson layers, keyed by a name property: 因此,我首先列出一个以name属性为键的geojson图层:

var layarr = {}
for (i=0;i<listOfNames.length;i++) {
  for (var prop in geojson['_layers']) {
    if (!geojson['_layers'].hasOwnProperty(prop)) {
        continue;
    }
    if (geojson['_layers'][prop]["feature"]["properties"]["name"]==listOfNames[i]) {
      layarr[listOfNames[i]] = geojson['_layers'][prop]
    }
  }
}

Then I call this function: 然后我调用此函数:

function highlight(name) {
  var laytouse = layarr[name]
  laytouse.setStyle(highlightStyle)
}

Nothing happens, not even an error. 什么都没有发生,甚至没有错误。

I already have a highlight function that works when the actual area on the map is moused over: 我已经有一个突出显示功能,当将鼠标放在地图上的实际区域上时,该功能可以工作:

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

Which is called in the following way: 通过以下方式调用:

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

geojson=L.geoJson(neighbData, {style: style,
onEachFeature: onEachFeature
}).addTo(mymap);

Even if I do something like this directly on the console: 即使我直接在控制台上执行以下操作:

layarr[name].setStyle({fillOpacity: 1});

I still get nothing. 我什么也没得到。 It seems like I'm getting the wrong layer somehow, but the layer has the expected setStyle() method and I'm not getting any console errors. 似乎我以某种方式得到了错误的图层,但是该图层具有预期的setStyle()方法,并且没有收到任何控制台错误。

edit: jsfiddle mockup: https://jsfiddle.net/6t8gnegf/ 编辑:jsfiddle样机: https ://jsfiddle.net/6t8gnegf/

gist solution 要点解决方案

Your question is very close to this one: Accessing Leaflet.js GeoJson features from outside . 您的问题非常接近这一问题: 从外部访问Leaflet.js GeoJson功能 The trick is that the geoJSON layer is in fact a layergroup, and you can therefore use the layergroup methods, such as getLayer(). 诀窍是geoJSON图层实际上是一个图层组,因此您可以使用图层组方法,例如getLayer()。

The idea is that you want to access your features based on their id. 这个想法是,您想根据其ID访问功能。 You first need to attach the polygon ID to your locations in the table (in the list in my gist example): 首先,您需要将多边形ID附加到表格中的位置(在我的要点示例的列表中):

function onEachFeature(feature, layer) {
    nhood = parseInt(feature.id);
    name = feature.properties.name;
    $('#neighborhood').append('<li value="' + nhood + '">'+name+'</li>');
    layer._leaflet_id = nhood;
    layer.on({
        mouseover: highlightFeature,
        mouseout: resetHighlight,
    });
}

Then when you mouseenter a location, you highlight the feature that matches the id of the location, as follows: 然后,当您用鼠标输入一个位置时,突出显示与该位置的ID匹配的功能,如下所示:

var hovered_id, layer;

$('#neighborhood li').on('mouseenter', function(e){
        hovered_id = e.target.value;
        layer = geojson.getLayer(hovered_id); //your feature id here
        layer.setStyle(highlightStyle);
    }).on('mouseout', function(e){
        geojson.resetStyle(layer);
    });

Please have a look at the gist I created , and you will see that the code is in reality much simpler than the one you initially shared. 请看一下我创建的要点 ,您将发现实际上该代码比您最初共享的代码简单得多。

EDIT: the leaflet ids must be unique, and the neighborhood names should be assigned to the feature ids. 编辑:传单ID必须是唯一的,并且邻域名称应分配给功能ID。 Below is the updated code: 下面是更新的代码:

function onEachFeature(feature, layer) {
    name = feature.properties.name;
    $('#neighborhood').append('<li data-value="' + name + '">'+name+'</li>');
    layer._leaflet_id = name;
    layer.on({
        mouseover: highlightFeature,
        mouseout: resetHighlight,
    });
}

var hovered_id, layer;

$('#neighborhood li').on('mouseenter', function(e){
        hovered_id = $(e.target).data('value');
        console.log(hovered_id);
        layer = geojson.getLayer(hovered_id); //your feature id here
        layer.setStyle(highlightStyle);
    }).on('mouseout', function(e){
        geojson.resetStyle(layer);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM