如何使用ImageOverlay通过LatLngBounds设置正确的图像尺寸?

时间:2017-02-25 21:22:19

标签: leaflet

我想使用ImageOverlays作为标记,因为我希望图像可以缩放比例。缩放时,标记图标总是会调整大小以保持其大小相同。

我的问题是,我无法弄清楚如何将像素转换为线索,因此我的图像不会被拉伸。

例如,我决定我的西南LatLng是[50,50]。我的图片尺寸为24px / 24px。

如何根据图像像素计算东北LatLng?

1 个答案:

答案 0 :(得分:3)

您可能正在寻找map conversion methods

特别是,您可以使用:

  • latLngToContainerPoint:给定地理坐标,返回相对于地图容器的相应像素坐标。
  • containerPointToLatLng:给定相对于地图容器的像素坐标,返回相应的地理坐标(对于当前缩放级别)。
// 1) Convert LatLng into container pixel position.
var originPoint = map.latLngToContainerPoint(originLatLng);
// 2) Add the image pixel dimensions.
// Positive x to go right (East).
// Negative y to go up (North).
var nextCornerPoint = originPoint.add({x: 24, y: -24});
// 3) Convert back into LatLng.
var nextCornerLatLng = map.containerPointToLatLng(nextCornerPoint);

var imageOverlay = L.imageOverlay(
  'path/to/image',
  [originLatLng, nextCornerLatLng]
).addTo(map);

演示:http://playground-leaflet.rhcloud.com/tehi/1/edit?html,output