简体   繁体   English

mapbox geojson属性在悬停时不可用

[英]mapbox geojson properties not available on hover

I'm using mapbox and loading multiple geojson layers on to the map, like so: 我正在使用mapbox并将多个geojson图层加载到地图上,如下所示:

...

var aLayer = L.mapbox.featureLayer('/a.geojson');
var bLayer = L.mapbox.featureLayer('/b.geojson');
var cLayer = L.mapbox.featureLayer('/c.geojson');

var layers = {
    LayerA: aLayer,
    LayerB: bLayer,
    LayerC: cLayer
}

...

layers.LayerA.addTo(map).on('ready', function(e) {
  map.fitBounds(LayerA.getBounds(), {animate: true});
});

L.control.layers(layers).addTo(map);

Each geojson feature contains a number of properties; 每个geojson功能都包含许多属性。 the title and description display correctly within the tooltips, for instance. 例如,标题和描述正确显示在工具提示中。

Where I'm having trouble is accessing these properties with other interactions. 我遇到麻烦的地方是通过其他交互访问这些属性。 I'm adjusting the style of each polygon on hover, like so: 我正在调整悬停时每个多边形的样式,如下所示:

LayerA.on('mouseover', function(e)
{
    e.layer.setStyle({
        weight: 2,
        fillOpacity: 0.7
    });

    if (!L.Browser.ie && !L.Browser.opera) {
        e.layer.bringToFront();
    }
});

LayerA.on('mouseout', function(e)
{
    e.layer.setStyle({
        weight: 0.5,
        fillOpacity: 0.5
    });
});

Any attempt to access the properties within the interactions, via e.layer or e.target , fails. 通过e.layere.target访问交互中的属性的任何尝试都将失败。 I assume the tooltip working means they are available, but can't work out where I've gone wrong...? 我认为工具提示有效意味着它们可用,但无法解决我出问题的地方...?

What you are doing is handling events of the featureLayer, not of the individual layers that are contained within the featureLayer. 您正在处理的是featureLayer的事件,而不是featureLayer中包含的各个图层的事件。 FeatureLayer is a group of layers, a layer for each feature within the collection you pass to it on initialization. FeatureLayer是一组图层,在初始化时传递给它的集合中每个要素的图层。 Take this collection for instance, renders three markers in one featureLayer: 以这个集合为例,在一个featureLayer中渲染三个标记:

var featureCollection = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "id": 1
        },
        "geometry": {
            "type": "Point",
            "coordinates": [0, 0]
        }
    },{
        "type": "Feature",
        "properties": {
            "id": 2
        },
        "geometry": {
            "type": "Point",
            "coordinates": [30, 30]
        }
      },{
        "type": "Feature",
        "properties": {
            "id": 3
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-30, -30]
        }
    }]
};

var featureLayer = L.mapbox.featureLayer(featureCollection).addTo(map);

Now if a hook a handler to the click event of featureLayer : 现在,如果将处理程序挂钩到featureLayerclick事件:

featureLayer.on('click', function (e) {
    console.log('Clicked featureLayer');
});

That will fire regardless of which marker i click. 无论我单击哪个标记,都将触发。 It isn't even aware of which marker i clicked, in there e.target will hold the instance of the featureLayer not of the individual layers contained within it. 甚至不知道我单击了哪个标记,在其中e.target将保存featureLayer的实例,而不包含其中包含的各个图层的实例。 Now if i hook clickhandler onto the click events of the indivual layers within featureGroup : 现在,如果我将clickhandler挂接到featureGroupfeatureGroup图层的click事件上:

featureLayer.eachLayer(function (layer) {
    layer.on('click', function (e) {
        console.log('Clicked feature ID: ' + e.target.feature.properties.id);
    });
});

Now in the click handler e.target holds the instance of the clicked marker, which has the feature object available as a property for you to use. 现在,在点击处理程序中, e.target保留了clicked标记的实例,该实例具有可作为属性使用的要素对象。

Here's a working example on Plunker: http://plnkr.co/edit/qwgDtQMRuR4XTcrnC1vf?p=preview 这是有关Plunker的工作示例: http ://plnkr.co/edit/qwgDtQMRuR4XTcrnC1vf?p=preview

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

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