首页 > 解决方案 > 标记geojson地图的特征:如何控制标签的位置

问题描述

我已经大大简化了代码,但问题仍然存在。

我有一个 geoJSON 地图,每个县都有一个特征。我想用县名称来标记县,该名称在属性中可用。我通过向 OnEachFeature 方法添加代码来做到这一点。标签的位置是错误的,并且在地图缩放时不会改变。

这是我的代码:

    function onEachFeature(feature, layer) {
        container = L.DomUtil.get('label-container')
        coords = feature.geometry.coordinates[0][0];
        coords2 = [coords[1],coords[0]];
        container.innerHTML = "label";
        var pos = map.latLngToLayerPoint(new L.latLng(coords2));
        op = new L.Point(pos.x,pos.y);
        L.DomUtil.setPosition(container, op);

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

标签: javascriptleafletgeojson

解决方案


推荐阅读