我有一个使用openlayers V5.3.0的节点映射应用程序。当我尝试将EPSG:4326的几何形状转换为使用proj4.defs定义的投影时,出现以下错误消息: TypeError:无法读取null的属性“ getCode”
如果我将openLayers更改为4.6.4版,则转换几何的代码将起作用。
还有其他人遇到过这个问题吗?
以下HTML演示了该问题。
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title>Projection Test</title>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol-debug.js"></script> -->
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
<script>proj4.defs("EPSG:28355","+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");</script>
<script>
$(document).ready(function() {
let gJSONOpts = {
dataProjection: "EPSG:3857",
featureProjection: "EPSG:28355"
};
let jsonPt = {
"type": "Feature",
"id": "GEO_BRM_STOPS.fid--79f50be5_16adc4d0ce7_6a6b",
"geometry": {
"type": "Point",
"coordinates": [16154413.50099648, -4988654.4460995]
},
"geometry_name": "OBJ"
};
let feature = new ol.format.GeoJSON().readFeature(jsonPt.geometry, gJSONOpts);
let tg = feature.getGeometry().transform('EPSG:28355', "EPSG:4326");
let out = 'Projected Coordinates: ' + tg.getCoordinates()[0] + ', ' +tg.getCoordinates()[1];
$("#mapvwr").html(out);
});
</script>
</head>
<body>
<div id="mapvwr">
</div>
</body>
</html>
将以上内容粘贴到html文件中,然后在浏览器中显示。
取消注释此行:
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol-debug.js"></script> -->
并注释掉以下行:
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
要查看正确的结果:
Projected Coordinates: 145.11756553931886, -40.83921285742692
答案 0 :(得分:0)
我发现了问题。
定义投影后,需要将此添加为脚本ol.proj.proj4.register(proj4);
。