I'm starting to play a bit with OpenLayers5
but I cannot even draw a simple point on the map. 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. Yet nothing appears on the TilesLayer
used as a base...
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:
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:
<!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);
before creating the feature.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.