1

我在 HTML 中有以下代码

<html>
<head>
<title>Vector Style Examples</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/css/ol.css">
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Teko" rel="stylesheet"> 
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<script>
var map = new ol.Map({
    view: new ol.View({center: ol.proj.transform([16.9278, 52.4044], 'EPSG:4326', 'EPSG:3857'), zoom:12}),
    layers: [new ol.layer.Tile({
            source: new ol.source.OSM()
    })],
    target:'map'
});

var marker = new ol.Feature({
    geometry: new ol.geometry.point(ol.proj.transform([16.9071388, 52.4901917], 'EPSG:4326', 'EPSG:3857')),
});

var markers = new ol.source.Vector({
    features: [marker]
});

var markerVectorLayer = new ol.layer.Vector({
    source: markers,
});
map.addLayer(markerVectorLayer);

</script>
</body>
</html>

它不显示任何点,只显示地图。在控制台中,我收到错误“ol.geom.point 不是构造函数”。此代码主要基于本教程 https://medium.com/attentive-ai/working-with-openlayers-4-part-2-using-markers-or-points-on-the-map-f8e9b5cae098

4

3 回答 3

4

改变

var marker = new ol.Feature({
    geometry: new ol.geometry.point(ol.proj.transform([16.9071388, 52.4901917], 'EPSG:4326', 'EPSG:3857')),
});

var marker = new ol.Feature({
    geometry: new ol.geom.Point(ol.proj.transform([16.9071388, 52.4901917], 'EPSG:4326', 'EPSG:3857')),
});

并在视图中将缩放从 12 更改为 11(否则,该点在此处但在初始视图之外,您需要缩小)

PS:不知道你从哪里得到ol.geometry.point的,因为它在你引用的教程中从未提到过

于 2018-08-24T12:11:46.510 回答
1

改变这个

 ol.geometry.point

对此

ol.geometry.Point

它会起作用。

于 2018-11-23T13:51:49.097 回答
0

好的,我知道为什么它不起作用了。您必须输入“ol.geom.Point”而不是“ol.geom.point”(大写)。

于 2018-08-25T12:37:05.470 回答