OpenLayers5中无法import解决方法

    本文参考此文:https://blog.csdn.net/qq_35505699/article/details/80858425

   OpenLayers5版本中引用ol.js的方式是import,如果直接复制官网例子到HTML中是无法运行的,由于import是ES6中的语法,因此无法直接copy运行,除非完整下载官网示例。

    根据不同情况,有2种解决方法:

    一、项目确定有联网环境。可以按照官网的例子,使用ES6语法,但是得参考https://github.com/openlayers/ol-webpack方式;

   二、无法确保项目有联网环境,建议下载官网的ol.js和ol.css,在HTML中引入,在new各种ol的类的时候,加上前缀包名。下面放一个例子,先放OpenLayers官网的第一个例子的代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Accessible Map</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v5.2.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>

    <style>
      a.skiplink {
        position: absolute;
        clip: rect(1px, 1px, 1px, 1px);
        padding: 0;
        border: 0;
        height: 1px;
        width: 1px;
        overflow: hidden;
      }
      a.skiplink:focus {
        clip: auto;
        height: auto;
        width: auto;
        background-color: #fff;
        padding: 0.3em;
      }
      #map:focus {
        outline: #4A74A8 solid 0.15em;
      }
    </style>
  </head>
  <body>
    <a class="skiplink" href="#map">Go to map</a>
    <div id="map" class="map" tabindex="0"></div>
    <button id="zoom-out">Zoom out</button>
    <button id="zoom-in">Zoom in</button>
    <script>
      import Map from 'ol/Map.js';
      import View from 'ol/View.js';
      import {defaults as defaultControls} from 'ol/control.js';
      import TileLayer from 'ol/layer/Tile.js';
      import OSM from 'ol/source/OSM.js';


      var map = new Map({
        layers: [
          new TileLayer({
            source: new OSM()
          })
        ],
        target: 'map',
        controls: defaultControls({
          attributionOptions: {
            collapsible: false
          }
        }),
        view: new View({
          center: [0, 0],
          zoom: 2
        })
      });

      document.getElementById('zoom-out').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom - 1);
      };

      document.getElementById('zoom-in').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom + 1);
      };
    </script>
  </body>
</html>

下面放改造之后的代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Accessible Map</title>
	<link rel="stylesheet" href="css/ol.css" />
	<script type="text/javascript" src="js/ol.js" ></script>

    <style>
      a.skiplink {
        position: absolute;
        clip: rect(1px, 1px, 1px, 1px);
        padding: 0;
        border: 0;
        height: 1px;
        width: 1px;
        overflow: hidden;
      }
      a.skiplink:focus {
        clip: auto;
        height: auto;
        width: auto;
        background-color: #fff;
        padding: 0.3em;
      }
      #map:focus {
        outline: #4A74A8 solid 0.15em;
      }
    </style>
  </head>
  <body>
    <a class="skiplink" href="#map">Go to map</a>
    <div id="map" class="map" tabindex="0"></div>
    <button id="zoom-out">Zoom out</button>
    <button id="zoom-in">Zoom in</button>
    <script>
//    import Map from 'ol/Map.js';
//    import View from 'ol/View.js';
//    import {defaults as defaultControls} from 'ol/control.js';
//    import TileLayer from 'ol/layer/Tile.js';
//    import OSM from 'ol/source/OSM.js';


      var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        target: 'map',
        controls: ol.control.defaults({
          attributionOptions: {
            collapsible: false
          }
        }),
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

      document.getElementById('zoom-out').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom - 1);
      };

      document.getElementById('zoom-in').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom + 1);
      };
    </script>
  </body>
</html>

 

转载自:https://blog.csdn.net/masonqiang/article/details/82626731

You may also like...