LeafletJS 工具提示偏移量取决于 properties.name

LeafletJS Tooltip offset depending on properties.name

我用 JSON 为多边形创建了一个图层,并尝试添加永久工具提示。问题是由于多边形的工作方式,工具提示会转到中心。这对大多数标签来说都很好,但只有一个标签处于非常尴尬的位置。我编写了下面的代码来仅抵消一个工具提示,但它正在将所有工具提示更改为名称“Ross Sea”并抵消它们。我做错了什么?

var seaLayer = L.geoJson(seaRegions, {
       style: function(feature) {
           return feature.properties.style
       },
       onEachFeature: function(feature, layer) {
        if (feature.properties.name = "Ross Sea") {
                    layer.bindTooltip(feature.properties.name, {className: "newContent", permanent: true, direction:"center", offset: L.point({x: -80, y: 80}) }); 
        } else {
            layer.bindTooltip(feature.properties.name, {className: "newContent", permanent: true, direction:"center"}); 
        }
       }
     });

您必须使用 == 而不是 =。您将 if 中的值 Ross Sea 初始化为所有功能。

更改为:

if (feature.properties.name == "Ross Sea") {
                    layer.bindTooltip(feature.properties.name, {className: "newContent", permanent: true, direction:"center", offset: L.point({x: -80, y: 80}) }); 
        } else {
            layer.bindTooltip(feature.properties.name, {className: "newContent", permanent: true, direction:"center"}); 
        }