gpt4 book ai didi

javascript - OpenLayers:从服务器获取 Map、View、TileLayer 和 OSM

转载 作者:行者123 更新时间:2023-12-01 14:55:25 25 4
gpt4 key购买 nike

我是使用 OpenLayers 的新手(一个开源 JavaScript 库,用于在 Web 浏览器中将 map 数据显示为滑动 map )。我将它与 Thymeleaf(一种 Java XML/XHTML/HTML5 模板引擎,可以在 Web 和非 Web 环境中工作)一起使用。

我试图重现此示例,但从服务器获取资源 https://openlayers.org/en/latest/examples/reprojection-wgs84.html

我有这个页面:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Title</title>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/build/ol.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/css/ol.css">

<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>

<div id="layout">


<div id="main">

<div class="content">
<div class="pure-g">

<div class="pure-u-1-1 pure-u-xl-10-24 pure-u-med-1">

<!-- Content right Wrap -->
<div class="content_r_wrap">

<!-- Devices Map Module -->
<div class="windowHead">
<h2>LOCATION INFO</h2>
</div>
<div class="windowContentMap">
<div id="map" class="map"></div>
<script th:inline="javascript" th:type="module">
/*<![CDATA[*/
import Map from '/css/Map';
import View from '/css/View';
import TileLayer from '/css/layer/Tile';
import OSM from '/css/source/OSM';

var map = new Map({
layers: [
new TileLayer({
source: new OSM()
})
],
target: 'map',
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
zoom: 2
})
});
/*]]>*/
</script>
</div>
</div>

</div>

</div>
</div>
</div>
</div>
</div>

</body>
</html>

我想知道如何从服务器获取这些对象:
  import Map from '/css/Map';
import View from '/css/View';
import TileLayer from '/css/layer/Tile';
import OSM from '/css/source/OSM';

最佳答案

不知道是什么意思,但如果“从服务器获取”意味着直接从编译源访问导入(无论是在您的服务器上还是在其他地方),这是如何做到的:

const Map = window.ol.Map;
const View = window.ol.View;
const TileLayer = window.ol.layer.Tile;
const OSM = window.ol.source.OSM;

var map = new Map({
layers: [
new TileLayer({
source: new OSM()
})
],
target: 'map',
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
zoom: 2
})
});
<link href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/css/ol.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/build/ol.js"></script>


<style>
.map {
width: 100%;
height: 400px;
}

</style>


<div id="layout">


<div id="main">

<div class="content">
<div class="pure-g">

<div class="pure-u-1-1 pure-u-xl-10-24 pure-u-med-1">

<!-- Content right Wrap -->
<div class="content_r_wrap">

<!-- Devices Map Module -->
<div class="windowHead">
<h2>LOCATION INFO</h2>
</div>
<div class="windowContentMap">
<div id="map" class="map"></div>
</div>
</div>

</div>

</div>
</div>
</div>
</div>

关于javascript - OpenLayers:从服务器获取 Map、View、TileLayer 和 OSM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62244109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com