单张向标记添加偏移量

时间:2015-03-16 15:06:23

标签: leaflet

我正在获取传单功能的中心以显示其标签。

然后,我在绑定标签时指定偏移量,使其显得更高且更直。

问题是我需要在entryJSON.getBounds().getCenter()代码中添加偏移量。

有没有办法在传单latlong对象中添加偏移量? (我可以想象像entryJSON.getBounds()。getCenter()。offset([ - 10,-57]),但这不起作用......)

 var marker = new L.marker(entryJSON.getBounds().getCenter(), { opacity: 0.01 });
 marker.bindLabel('whatever here', {noHide: true, className: "info", offset: [-10, -57] });

2 个答案:

答案 0 :(得分:8)

标签的偏移属性适用于像素,L.LatLng对象适用于坐标而非像素。您可以做的是使用L.Map的转换方法并将当前坐标位置转换为像素位置,更改然后转换回来:

var latLng = L.latLng([0,0]),

var point = map.latLngToContainerPoint(latLng);

var newPoint = L.point([point.x - 10, point.y - 57]);

var newLatLng = map.containerPointToLatLng(newPoint);

示例:http://plnkr.co/edit/LeNqz8?p=preview

参考:http://leafletjs.com/reference.html#map-latlngtocontainerpoint

答案 1 :(得分:1)

更简单的一个:https://leafletjs.com/examples/custom-icons/

var greenIcon = L.icon({
    iconUrl: 'leaf-green.png',
    shadowUrl: 'leaf-shadow.png',

    iconSize:     [38, 95], // size of the icon
    shadowSize:   [50, 64], // size of the shadow
    iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
    shadowAnchor: [4, 62],  // the same for the shadow
    popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
});