检索 Leaflet 的当前比例值
是否可以获取Leaflet组件当前的缩放值?
Is it possible to get the current scale value of the Leaflet component?
上图显示了我想通过某种方法检索的300 公里"或100 英里".现有文档仅显示如何使用特定选项添加比例控制:http://leafletjs.com/reference.html#control-scale
The image above shows "300 km" or "100 miles" that i would like to retrieve by a method. The existing documentation does only show how to add the scale control with specific options: http://leafletjs.com/reference.html#control-scale
推荐答案
在低缩放级别(当你看到世界的很大一部分时)小心缩放.
Be careful with the scale at low zoom levels (when you see a large portion of the world).
您看到的比例实际上对地图视图的中心水平线有效.地图的一角,它所在的位置,甚至有点不对劲!
The scale that you see is actually valid for the center horizontal line of your map view. It is even slightly wrong for the corner of the map, where it is placed!
如果您只是想在其他地方复制"该 visual 比例,您可以简单地创建另一个 Scale Control 并提取其 HTML 容器,而不是将其嵌入到您的地图中:
If you just want to "duplicate" that visual scale somewhere else, you could simply create another Scale Control and extract its HTML container instead of embedding it to your map:
document.getElementById("myNewContainerId").appendChild(
L.control.scale(options).onAdd(map)
);
如果您想读取缩放控件的实际像素长度和文本,您可以通过内部 _mScale.style.width
和 _mScale.innerHTML
属性检索它们的比例控制.如果您想要英制值而不是公制值,请将 _mScale
替换为 _iScale
.
If you want to read the actual pixel length and text of the Scale Control, you could retrieve them through the internal _mScale.style.width
and _mScale.innerHTML
properties of the Scale Control. Replace _mScale
by _iScale
if you want the imperial values instead of the metric ones.
否则,如果您希望能够测量地图上两点之间的距离,您应该使用 myLatLng.distanceTo(otherLatLng)
方法,这将更加准确,因为它不仅会在 myLatLng
实际使用正确的比例纬度,但如果将其放置在不同的纬度,也可以纠正沿 otherLatLng
路径可能存在的不同比例.
Otherwise, if you want to be able to measure some distance between 2 points on the map, you should rather use the myLatLng.distanceTo(otherLatLng)
method, which would be far more accurate, as it would not only use the correct scale at the myLatLng
actual latitude, but also correct for the possible different scale along the path to otherLatLng
if it is placed at a different latitude.
返回到使用 Haversine 公式计算的给定 LatLng 的距离(以米为单位).请参阅 维基百科上的说明
Returns the distance (in meters) to the given LatLng calculated using the Haversine formula. See description on wikipedia
相关文章