- 有关坐标转换的包都在
ol/proj
中 - 其中
EPSG:3857
和EPSG:4326
坐标系之间的转换有transform、fromLonLat、toLonLat - fromLonLat是将
EPSG:4326
转换为EPSG:3857
;而toLonLat是将EPSG:3857
转换为EPSG:4326
- transform的用法是
transform([坐标], 'EPSG:3857', 'EPSG:4326');
transform([坐标], 'EPSG:4326', 'EPSG:3857');
- transform的转换和fromLonLat、toLonLat的区别是:transform转换之后的经度可能是几千,而toLonLat转换之后的经度位于[-180, 180]之间。
- 因为地图是不断拓展的,一个地方一直往右拖,同一个地方的经度是±360的变化
//3857=>4326
let coordinate = transform(
evt.coordinate,
"EPSG:3857",
"EPSG:4326"
)
let x = coordinate[0];
let flag = (x + 180) % 360;
if(flag > 0) {
flag -= 180;
} else {
flag += 180;
}
coordinate[0] = flag;
//等价于
let coordinate = toLonLat(evt.coordinate);