图标标记不在中心。传单js

Icon marker not center. Leaflet js

如何使标记图标居中。创建后,它会离开单击的位置,并且也会显示工具提示 Screenshot

我的代码:

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'});
}
 

调查tutorial you need to define the iconAnchor

如何找出正确的锚点:

在 latlng 上创建一个默认标记,并使用具有相同 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);