我发现了这个使用OpenLayers渲染带有标记的地图的较旧示例。该示例引用的是openlayers.js,但最新版本5.3.0使用ol.js。
我已将脚本更改为引用ol.js。由于版本更改,这些调用也发生了变化,因此我也将这些调用更正为ol.proj。但是,我在控制台(Chrome)中遇到JavaScript错误:
未捕获的TypeError:t.getCode不是函数
在Le(proj.js:419)
在大江(proj.js:442)
在Pe(proj.js:461) 在index.html:16
我不知道为什么会这样。
示例的旧版本:
<!DOCTYPE HTML>
<html>
<head>
<title>OpenLayers Simplest Example</title>
</head>
<body>
<div id="Map" style="height:250px"></div>
<script src="OpenLayers.js"></script>
<script>
var lat = 47.35387;
var lon = 8.43609;
var zoom = 18;
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(lon, lat).transform( fromProjection, toProjection);
map = new OpenLayers.Map("Map");
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(position));
map.setCenter(position, zoom);
</script>
</body>
</html>
我的修订版本:
<!DOCTYPE HTML>
<html>
<head>
<title>OpenLayers Simplest Example</title>
</head>
<body>
<div id="Map" style="height:250px"></div>
<script src="./build/ol.js"></script>
<script>
var lat = 47.35387;
var lon = 8.43609;
var zoom = 18;
var fromProjection = new ol.proj.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new ol.proj.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new ol.proj.toLonLat(lon, lat).transform( fromProjection, toProjection);
map = new ol.Map("Map");
var mapnik = new ol.Layer.OSM();
map.addLayer(mapnik);
var markers = new ol.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new ol.Marker(position));
map.setCenter(position, zoom);
</script>
</body>
</html>
预期结果将是渲染的地图,并在地图上放置标记。
我得到的是上面提到的未捕获的类型错误。