【发布时间】:2020-05-18 20:17:34
【问题描述】:
一段时间以来,我一直在尝试让开放层在我的 Eclipse Web 开发环境中工作。 Open Layers 站点上的所有设置说明都涉及使用 npm。但是,它们也有一个指向包含 ol.js、ol.js.map、ol.css 和 ol.css.map 的发行版的链接。在他们的workshop 中,他们有一个如下所示的 javascript 文件:
import 'ol/ol.css';
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import XYZSource from 'ol/source/XYZ';
import {fromLonLat} from 'ol/proj';
new Map({
target: 'map-container',
layers: [
new TileLayer({
source: new XYZSource({
url: 'http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg'
})
})
],
view: new View({
center: fromLonLat([0, 0]),
zoom: 2
})
});
我正在苦苦挣扎的是如何修改它以使用 java 脚本文件而不是 npm 库。我想这部分将是唯一会改变的部分。
import 'ol/ol.css';
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import XYZSource from 'ol/source/XYZ';
import {fromLonLat} from 'ol/proj';
我尝试将文件放在文件夹中并将导入代码更改为:
import './lib/ol.css'
import {Map, View} './lib/ol.js'
import TileLayer from './lib/ol.js';
import XYZSource from './lib/ol.js';
import {fromLonLat} from './lib/ol.js';
那没用。可能是因为我不完全了解发生了什么。我读到一个 js 文件可以用作库,所以我想我可以引用该文件。
他们也有一个 html 文件作为教程的一部分,因为我没有使用 npm,我是否也必须更改它?我假设因为他们指示将 js 文件和 html 文件都放在项目的根目录中,所以他们可以引用另一个(因为 js 文件将导入 css 而 html 文件将使用它),但我不是完全确定。这是供参考的html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OpenLayers</title>
<style>
html, body, #map-container {
margin: 0;
height: 100%;
width: 100%;
font-family: sans-serif;
}
</style>
</head>
<body>
<div id="map-container"></div>
</body>
</html>
谁能帮帮我?感谢您的宝贵时间。
【问题讨论】:
-
您必须学习 webpack 并学习如何使用它来捆绑和构建 JavaScript 应用程序。
标签: javascript html npm openlayers