请问大家知道有什么方法可以判断投影是4326还是3857,拿到一个数据之后进行判断。
使用OpenLayers库可以判断4326和3857投影的方法:
首先要确定数据的投影信息。
然后,使用OpenLayers库的ol.proj.get(<投影代码>)函数可以获得投影的相关信息,投影代码分别是'EPSG:4326'和'EPSG:3857'。
如果获得的投影信息是'EPSG:4326',则代表数据使用的是4326投影。如果是'EPSG:3857',则代表数据使用的是3857投影。
var projectionCode = 'EPSG:4326';
var projection = ol.proj.get(projectionCode);
if(projection) {
console.log("The projection is: " + projectionCode);
}
else {
console.log("Invalid projection code: " + projectionCode);
}
不知道你这个问题是否已经解决, 如果还没有解决的话: let country = new Style({
stroke: new Stroke({
color: "rgba(164, 162, 174)",
width: 2
}),
fill: new Fill({
color: "rgba(233, 229, 216)"
})
});
let vector4326 = new VectorTileLayer({
// 矢量切片的数据源
source: new VectorTileSource({
projection: "EPSG:4326",
format: new MVT({
idProperty: "iso_a3"
}),
tileGrid: createXYZ({
extent: olProj.get("EPSG:4326").getExtent(),
maxZoom: 22
}),
// 矢量切片服务地址
tileUrlFunction: function(tileCoord) {
const url =
"https://ahocevar.com/geoserver/gwc/service/tms/1.0.0/" +
"ne:ne_10m_admin_0_countries@EPSG%3A4326@pbf/" +
(tileCoord[0] - 1) +
"/" +
tileCoord[1] +
"/" +
(Math.pow(2, tileCoord[0] - 1) - 1 - tileCoord[2]) +
".pbf";
return url;
}
}),
style: country
});
let map = new Map({
target: "map",
layers: [
vector4326,
// 加载瓦片网格图层
// new TileLayer({
// //瓦片网格数据源
// source: new TileDebug({
// //获取瓦片网格信息
// // tileGrid: vector4326.getSource().getTileGrid()
// })
// })
],
controls: [
new MousePosition({
coordinateFormat: createStringXY(6) // 鼠标位置
})
],
view: new View({
projection: "EPSG:4326", // pbf4326格式需要使用4326坐标系
center: [114.1564839, 24.61548857],
zoom: 2
})
});
效果图如下: