簡體   English   中英

OpenLayers功能未呈現

[英]OpenLayers Feature is not getting rendered

我開始使用OpenLayers5但是我什至無法在地圖上畫一個簡單的點。 我按照教程學習並掌握了一些示例,但是它們始終嘗試實現我想完成的更復雜的事情:在地圖上畫一個點。

我肯定會丟失某些東西,但看不到它是什么。 我創建了map並添加了一個VectorLayer和一個VectorSource其中包含要繪制的單點。 但是在用作基礎的TilesLayer上沒有任何顯示...

非常歡迎您提供有關我所缺少的提示: 我確定我缺少一些小細節,但我無法克服。

這是我的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
    })
  });

}

和簡單的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>

您應該通過fromLatLon(pointCoords);將點坐標從(Lon,Lat)轉換為WebMercator fromLatLon(pointCoords); 在創建功能之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM