[英]OpenLayers Feature is not getting rendered
I'm starting to play a bit with OpenLayers5
but I cannot even draw a simple point on the map. 我开始使用OpenLayers5
但是我什至无法在地图上画一个简单的点。 I followed the tutorials and had a grasp at some of the examples but they always try to achieve more complex things that what I wanted to accomplish: Draw a single point on the map. 我按照教程学习并掌握了一些示例,但是它们始终尝试实现我想完成的更复杂的事情:在地图上画一个点。
I'm definitely missing something but I cannot see what it is. 我肯定会丢失某些东西,但看不到它是什么。 I created the map
and added a VectorLayer
with a VectorSource
containing my single point to be drawn. 我创建了map
并添加了一个VectorLayer
和一个VectorSource
其中包含要绘制的单点。 Yet nothing appears on the TilesLayer
used as a base... 但是在用作基础的TilesLayer
上没有任何显示...
Hints/Directions on what I'm missing are very much welcome :). 非常欢迎您提供有关我所缺少的提示: I'm sure I'm missing some small detail but I cannot overcome it. 我确定我缺少一些小细节,但我无法克服。
This is my JS
file: 这是我的JS
文件:
import 'ol/ol.css';
import {Map,View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import {fromLonLat} from 'ol/proj';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import VectorSource from 'ol/source/Vector';
import OSM from 'ol/source/OSM';
document.addEventListener('DOMContentLoaded', initialize, false);
function initialize() {
var mapCenter = [-5.93, 43.52]; // Long, lat
var pointCoords = [-5.93, 43.52];
var features = [
new Feature(new Point(pointCoords))
];
const map = new Map({
target: 'mapCanvas',
layers: [
new TileLayer({
source: new OSM()
}),
new VectorLayer({
source: new VectorSource({
features: features
})
})
],
view: new View({
center: fromLonLat(mapCenter),
zoom: 12
})
});
}
And the simple HTML file: 和简单的HTML文件:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/posiciones.css">
</head>
<body>
<div id="mapCanvas"></div>
<div id="sideMenu"></div>
<script src="js/posiciones.js"></script>
</body>
You should transform the point coordinates from (Lon, Lat) to WebMercator via fromLatLon(pointCoords);
您应该通过fromLatLon(pointCoords);
将点坐标从(Lon,Lat)转换为WebMercator fromLatLon(pointCoords);
before creating the feature. 在创建功能之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.