【问题标题】:OpenLayers Feature is not getting renderedOpenLayers 功能未呈现
【发布时间】:2019-01-01 02:32:23
【问题描述】:

我开始玩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>

【问题讨论】:

    标签: javascript openlayers openlayers-5


    【解决方案1】:

    在创建特征之前,您应该通过fromLatLon(pointCoords); 将点坐标从 (Lon, Lat) 转换为 WebMercator。

    【讨论】:

    • 你好 RalphL,确实是这个问题,我需要先转换点坐标......非常感谢你指出这一点。节日快乐:)
    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多