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

标签 javascript html css thymeleaf openlayers

我是使用 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/

相关文章:

javascript - 如何计算 Parse 平台上的 BeforeSave CloudCode 中的总用户数

javascript - 使用其他语句调用 setState 方法

html - 使用两个溢出 : hidden then overflow-y: scroll not working as expected 的 div

html - 获取左侧边栏背景一直向左延伸

html - 更改文本输入字段时使用 AngularJS 对标签进行样式化

javascript - 如何使用 JavaScript 或 jQuery 和正则表达式重写链接?

javascript - `Node.js` 和/或其他 Javascript 分支相对于非 JS 框架(Rails、Django...)的性能、稳定性和速度

html - 如何在 CSS 中制作没有图像的自定义选择选项菜单样式?

html - 内容垂直对齐前的 CSS

javascript - 即使我刷新,文本框值仍然存在?