向标记添加偏移量的传单
Leaflet adding an offset to marker
我正在获取传单功能的中心以显示其标签。
然后,我在绑定标签时指定了一个偏移量,使其显示在右上角。
问题是我需要在 entryJSON.getBounds().getCenter()
代码中添加偏移量。
有没有办法在 leaflet 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] });
标签的偏移量 属性 适用于像素。 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
更简单的一个: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
});
我正在获取传单功能的中心以显示其标签。
然后,我在绑定标签时指定了一个偏移量,使其显示在右上角。
问题是我需要在 entryJSON.getBounds().getCenter()
代码中添加偏移量。
有没有办法在 leaflet 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] });
标签的偏移量 属性 适用于像素。 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
更简单的一个: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
});