問題描述
是否可以獲取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
這篇關于檢索 Leaflet 的當前比例值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!