检索传单的当前比例值 [英] Retrieve current scale value of 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!
如果您只是想复制" 视觉在其他地方缩放,则可以简单地创建另一个缩放控件并提取其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
属性检索它们.如果要使用英制而不是公制,请用_iScale
替换_mScale
.
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.
否则,如果您希望能够测量地图上2个点之间的距离,则应该使用 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的距离(以米为单位).参见关于维基百科的说明
这篇关于检索传单的当前比例值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!