简体   繁体   English

图标标记不在中心。 Leaflet 杰

[英]Icon marker not center. Leaflet js

how to make a marker icon centered.如何使标记图标居中。 When created, it moves away from clicked place and when tooltip is displayed too Screenshot创建后,它会离开单击的位置,并且也会显示工具提示屏幕截图

My code:我的代码:

L.geoJSON(data,{
          pointToLayer: pointToLayer,
          //...
})

function pointToLayer(feature, latlng) {
    return L.marker(latlng, {icon: leafletIcon(feature)});
}
function leafletIcon(feature){
   if(condition)
     return L.icon({iconUrl: 'images/***.png'});
   else if(condition)
     return L.icon({iconUrl: 'images/***.png'});
}
 

Look into the tutorial you need to define the iconAnchor查看您需要定义iconAnchor教程

How to find out, the correct anchor:如何找出正确的锚点:

Create a default marker on latlng and one with the custom icon with the same latlng.在 latlng 上创建一个默认标记,并使用具有相同 latlng 的自定义图标创建一个标记。 Then you see in which direction the custom icon needs to be moved, because the default marker is centered correct on the latlng.然后您会看到自定义图标需要朝哪个方向移动,因为默认标记在 latlng 上正确居中。

var defaultMarker = L.marker([0,0]).addTo(map);
var customMarker  = L.marker([0,0],{icon: L.icon({iconUrl: 'images/***.png', iconAnchor: [-3,9], popupAnchor: [0,0], tooltipAnchor: [0,0]}) }).addTo(map);

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

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