問題描述
我使用的是最新的穩(wěn)定版本 Leaflet 0.7.7,并且我使用的是繼承自 L.CRS.Simple
的自定義 CRS.
I'm using Leaflet 0.7.7, latest stable release, and I'm using a custom CRS inherited from L.CRS.Simple
.
CRS:
它與 Simple CRS 非常相似,但 c
設置為 1
(在 Simple 中,c
設置為 -1
).
It is very similar to Simple CRS but with c
set to 1
(in Simple, c
is set to -1
).
L.CRS.XY = L.Util.extend({}, L.CRS.Simple, {
code: 'XY',
projection: L.Projection.LonLat,
transformation: new L.Transformation(1, 0, 1, 0)
});
這個 CRS 的目標是擁有一個真正的 {x, y}
地圖系統(tǒng),其中 y
在到達地圖底部時變得更高(如位圖).
The goal of this CRS is to have a real {x, y}
map system where y
becomes higher when reaching the bottom of the map (like a bitmap).
要測試的代碼:
var southWest = L.latLng(133, 0);
var northEast = L.latLng(0, 170);
var bounds = L.latLngBounds(southWest, northEast);
document._map.setMaxBounds(bounds);
document._map.fitBounds(bounds);
document._markers[68].setLatLng(bounds.getNorthEast());
console.info('southWest', southWest);
// L.LatLng {lat: 133, lng: 0}
console.info('northEast', northEast);
// L.LatLng {lat: 0, lng: 170}
console.info('bounds', bounds);
// L.LatLngBounds (
_northEast: L.LatLng {lat: 133, lng: 170 }
_southWest: L.LatLng {lat: 0, lng: 0 }
)
實際上,由于我有一個自定義 CRS,我認為 這些行 是問題的根源,因為在平面 {x, y}
系統(tǒng)中最大值和最小值不正確(即使我更喜歡使用點",但我只能使用LatLng"對象:confused:).
Actually, as I have a custom CRS, I think these lines are the source of the problem because maximum and minimum values are not correct in a plane {x, y}
system (even if I would prefer to use "Point" but I can only use "LatLng" objects :confused: ).
所以實際上,這個問題似乎很明顯來自我的代碼,但實際上我想為此找到一個不需要我切換到 L.CRS.Simple 的解決方案
其中 y
坐標在地圖頂部變得更高.
So actually, it seems it is obvious that this issue comes from my code, but actually I'd like to find a solution for this that wouldn't need me to switch to L.CRS.Simple
in which y
coordinates get higher on top of the map.
那么在這個簡單的自定義投影中使用邊界的解決方案是什么?
So what's the solution to use bounds with this simple custom projection?
推薦答案
聽起來 Leaflet 在內(nèi)部假設 y 點在下降時增加(如圖像中),而緯度應該相反(即下降時減少),如 min、max、<= 和 >=比較是硬編碼的.
It sounds like Leaflet internally assumes that y points increase when going down (like in images), and it should be the opposite for latitude (i.e. decrease when going down), as the min, max, <= and >= comparisons are hard-coded.
因此,可能不可能組成一個 CRS,它會給你一個與 Y 點方向相同的緯度.除非您準備好修改 Leaflet 庫中比較緯度的每個函數(shù)……
So there is probably no possibility to make up a CRS that will give you a latitude in the same direction as the Y points. Except if you are ready to modify every function that compares latitudes within Leaflet library…
如果您每次必須提供坐標時都使用中間轉換函數(shù),而當您必須讀取坐標時則相反,您仍然可以隨意操作"數(shù)字.
You may still be able to "manipulate" numbers like you wish, if you use an intermediate conversion function every time you have to provide coordinates, and the opposite when you have to read coordinates.
這也讓您有機會將緯度 (y) 和經(jīng)度 (x) 的順序恢復為 [x, y].
That would also give you the opportunity to revert the latitude (y) and longitude (x) order to be [x, y].
例如:
function revertLat(x, y) {
return [-y, x];
}
演示:http://jsfiddle.net/ve2huzxw/101/
原答案:
還有什么理由不直接自定義 L.LatLngBounds
以使其符合您的需要?
Any reason for not directly customizing L.LatLngBounds
as well, so that it fits your need?
首先,如果您并不完全需要 southWest 和 northEast,而只需要 corners 作為邊界,則可以按原樣使用 L.LatLngBounds
.例如,即使拐角不完全是西南和東北,所有 Leaflet 方法也會繼續(xù)工作:map.fitBounds
、L.imageOverlay
等應該可以正常工作.
First of all, if you do not exactly need southWest and northEast, but just corners for your bounds, you can use L.LatLngBounds
as is. For instance, all Leaflet methods would keep working even if the corners are not exactly southWest and northEast: map.fitBounds
, L.imageOverlay
etc. should work fine.
否則,您將不得不在 L.LatLngBounds
中自定義許多方法(擴展、填充、包含、相交)以恢復最小/最大和 <=/>= 比較.
Otherwise, you would have to customize many methods in L.LatLngBounds
(extend, pad, contains, intersects) to revert the min / max and <= / >= comparisons.
這篇關于Leaflet LatLngBounds 與更簡單的 CRS &投影的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!