不止一本“oneachfeature”传单

时间:2014-01-10 08:23:00

标签: javascript gis leaflet

我正在尝试在一个图层上组合两个功能,但不能同时将它们放在工作中。一个是突出显示光标悬停在上面,另一个是在弹出窗口中获取信息。我使用本指南突出显示:http://leafletjs.com/examples/choropleth.html(添加交互部分,尝试将其合并到工作信息弹出层)。此外,它在悬停时突出显示,但如果它会在点击时突出显示会很好。 代码:

    function style(feature) {
    return {
        fillColor: 'blue',
        weight: 2,
        opacity: 1,
        color: 'grey',
        dashArray: '3',
        fillOpacity: 0.7
    };
}

    L.geoJson(piirid, {style: style});

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

        layer.setStyle({
            weight: 5,
            color: '#666',
            dashArray: '',
            fillOpacity: 0.7
        });

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

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

    var geojson;
    // ... our listeners
    geojson = L.geoJson(piirid);

    function zoomToFeature(e) {
        map.fitBounds(e.target.getBounds());
    }

    function onEachFeature3(feature, layer) {
        layer.on({
            mouseover: highlightFeature,
            mouseout: resetHighlight,
            //click: zoomToFeature
        });
    }

    geojson = L.geoJson(piirid, {
        style: style,
        onEachFeature: onEachFeature,
        onEachFeature: onEachFeature3
    });

    function onEachFeature(feature, layer) {
        if (feature.properties) {
            layer.bindPopup("<br><b><big><u>Aadresss: " + feature.properties.L_AADRESS + "</br></b></big></u><br> <b>Maakond:&nbsp;</b>" + feature.properties.MK_NIMI
            + " <br><br>", {"offset":  [200, -50]});
        }

}

谢谢,Kristjan

1 个答案:

答案 0 :(得分:2)

结合这两个函数,它们使用相同的参数,也可以作为一个函数进行计算。

 function style(feature) {
        return {
            fillColor: 'blue',
            weight: 2,
            opacity: 1,
            color: 'grey',
            dashArray: '3',
            fillOpacity: 0.7
        };
    }

    L.geoJson(piirid, {style: style});

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

        layer.setStyle({
            weight: 5,
            color: '#666',
            dashArray: '',
            fillOpacity: 0.7
        });

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

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

    var geojson;
    // ... our listeners
    geojson = L.geoJson(piirid);

    function zoomToFeature(e) {
        map.fitBounds(e.target.getBounds());
    }

    function onEachFeature3(feature, layer) {
        layer.on({
            mouseover: highlightFeature,
            mouseout: resetHighlight,
            //click: zoomToFeature
        });
        if (feature.properties) {
            layer.bindPopup("<br><b><big><u>Aadresss: " + feature.properties.L_AADRESS + "</br></b></big></u><br> <b>Maakond:&nbsp;</b>" + feature.properties.MK_NIMI
            + " <br><br>", {"offset":  [200, -50]});
        }
    }

    geojson = L.geoJson(piirid, {
        style: style,
        onEachFeature: onEachFeature3
    });