传单标记工具提示居中

Leaflet Marker Tooltip centered

我想在标记的中心(内部)显示工具提示。

背景

我想显示每个标记的计数(第一个 -> 最后一个),我发现我可以用工具提示来做到这一点(任何更好的建议)

所以我的标记现在看起来像这样,

var marker = L.marker(latlng, {icon: blueIcon}).bindTooltip(feature.properties.count, 
                    {
                permanent: true, 
                direction: 'right'
            });

我找不到关于方向 centered 或类似方向的任何进一步文档。

工具提示提供了查看哪个是第一个和最后一个标记的功能,但这似乎不是最佳做法。

所以我的问题:

例子

当前:

想要:

(这里我会让背景不可见,所以我只能看到文字。)

我发现您可以添加 direction: center。 参考:http://leafletjs.com/reference-1.0.0.html#tooltip-direction

我已经用 DivIcon

解决了我的问题
var numberIcon = L.divIcon({
                    className: "number-icon-default",
                    iconSize: [25, 41],
                    iconAnchor: [10, 44],
                    popupAnchor: [3, -40],
                    html: feature.properties.count        
              });

与 css 喜欢

    .number-icon-default
{
    background-image: url("#{request.contextPath}/lib/leaflet/images/marker-icon.png");
    text-align:center;
    color:Black;   
    text-shadow: 1px 1px #000000;
    font-size: large;
    font-weight: bold;
}

结果

Is there a better solution than tooltip?

不一定 "better"(这取决于您的确切要求),但更简单的解决方案是使用带有一些 HTML 文本的标记图标。

许多传单plugins can provide you with such feature, e.g. Leaflet.extra-markers

var map = L.map('map').setView([48.86, 2.35], 11);

var redMarker = L.ExtraMarkers.icon({
  number: '42',
  icon: 'fa-number',
  markerColor: 'red',
  shape: 'square',
  prefix: 'fa'
});

L.marker([48.86, 2.35], {
  icon: redMarker
}).addTo(map);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
<!-- Leaflet assets -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet-src.js" integrity="sha512-IkGU/uDhB9u9F8k+2OsA6XXoowIhOuQL1NTgNZHY1nkURnqEGlDZq3GsfmdJdKFe1k1zOc6YU2K7qY+hF9AodA==" crossorigin=""></script>

<!-- Leaflet.extramarkers assets -->
<link rel="stylesheet" href="https://unpkg.com/leaflet-extra-markers@1.0.6/src/assets/css/leaflet.extra-markers.css" />
<script src="https://unpkg.com/leaflet-extra-markers@1.0.6/src/assets/js/leaflet.extra-markers.js"></script>

<div id="map" style="height: 200px"></div>