【问题标题】:Why examples don't work? (a struggle with imports)为什么示例不起作用? (与进口的斗争)
【发布时间】:2018-06-29 03:43:06
【问题描述】:

在页面https://openlayers.org/en/latest/examples/image-vector-layer.html上,我将地图下的HTML代码复制到/tmp/a.html并运行firefox /tmp/a.html

起初有两个问题似乎很容易解决:

  1. 语法错误:导入声明只能出现在模块的顶层
  2. 未声明 HTML 文档的字符编码。文件...

修复它:

  1. <script> 替换为<script type="module">
  2. <meta charset="UTF-8"> 添加到<head></head>

但是如何处理第三个错误?

TypeError: Error resolving module specifier: ol/Map.js

我有 Firefox 60.0.1。

那么,示例中的 HTML 代码是按照我的习惯使用的,还是我误解了什么?

我在import Map from ol/Map.js 的代码中需要什么?

(我试图重新提出问题,但如果我仍然应该得到负面排名,请解释原因。谢谢)

【问题讨论】:

  • 我在尝试帮助您时遇到了同样的错误,此页面自更新以来可能不会更改,通常示例与复制粘贴效果很好
  • 我也在尝试直接在浏览器中使用 ol es6 模块。我只有部分答案。要import Map from ol/Map.js,请使用您的 ol 模块的相对或完全限定路径。例如import Map from './node_modules/ol/Map.js'; 之后我必须编辑几个 ol 模式并提供 node_modules/rbush/rbush.js 的相对路径。不幸的是,我仍然遇到与导入 rbush 相关的错误。

标签: openlayers


【解决方案1】:

这是因为最新版本的 OpenLayers (V5.0) 发生了一些变化。 现在的示例基于 ES6 模块,而之前有另一种方法

您可以将the "simple" v4.6.5 sample"simple" master sample 进行比较

使用<script type="module"> 是不够的,因为它在执行import Map from ol/Map.js 时无法解决依赖关系

至少有3种方法:

  1. 使用 5.0.0 版创建 Openlayers 示例的常用方法是使用像 Webpack 或 Parcel 这样的打包程序。有a tutorial for this

  2. 我还用this sample调查了JSPM

  3. 您始终可以使用旧方法,例如在版本 4.6.5 中使用 this other official tutorial 而不使用 import

对于解决方案 1,您可以使用 codesandbox.io 来避免设置本地 parcel/webpack 环境,如 this tweet 所示。

我知道重构示例代码的工作正在取得进展,我还为 codeandbox.io 提交了一些建议,例如 https://github.com/openlayers/openlayers/issues/8324

【讨论】:

  • 我有一个类似的问题,因为我有一个基于 OpenLayers4 的程序,其类扩展了 ol.Tile。对于 OpenLayers6,ol 对象中不存在 ol.Tile。关于如何处理的任何建议?
【解决方案2】:

有同样的问题。 openlayers 很出色,v5 的例子不是 :(

例如https://openlayers.org/en/latest/examples/animation.html

我对 v5(.3.0) 示例的修复:

    <!-- ADD build-REFERENCE for v5(.3.0) // github.com/openlayers/openlayers/ -->
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>

    <script>
    // CONVERT imports to var
    var Map                 = ol.Map;        //~ import Map from 'ol/Map.js';
    var View                = ol.View;       //~ import View from 'ol/View.js';
    var {easeIn, easeOut}   = ol.easing;     //~ import {easeIn, easeOut} from 'ol/easing.js';
    var TileLayer           = ol.layer.Tile; //~ import TileLayer from 'ol/layer/Tile.js';
    var {fromLonLat}        = ol.proj;       //~ import {fromLonLat} from 'ol/proj.js';
    var OSM                 = ol.source.OSM; //~ import OSM from 'ol/source/OSM.js';        
    // ...

过程:在示例页面上使用“复制”,“粘贴”到新的 html 文件。修改如上。在 Firefox 中运行。

【讨论】:

  • 非常感谢,请参阅stackoverflow.com/a/56549364/903783 了解更多情况,例如从 'ol/layer.js' 导入 {Tile as TileLayer, Vector as VectorLayer} 以及如何转换它们(也许有更好的方法,即兴创作)
【解决方案3】:

根据 lou 的回答,这是我刚刚为 Marker animation example 所做的修复:

<head>
<meta charset="UTF-8">
<title>Marker Animation</title>
<link rel="stylesheet" href="https://openlayers.org/en/v5.3.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>

<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
</head>

<body>
<div id="map" class="map"></div>

<label for="speed">
  speed:&nbsp;
  <input id="speed" type="range" min="10" max="999" step="10" value="60">
</label>

<button id="start-animation">Start Animation</button>

<script>
  var Feature = ol.Feature; //import Feature from 'ol/Feature.js';
  //var Map = ol.Map; //import Map from 'ol/Map.js'; //commented-out, use ol.Map in code instead of Map, internal JS Map object causes conflicts in newer browsers
  var View = ol.View; //import View from 'ol/View.js';
  var Polyline = ol.format.Polyline; //import Polyline from 'ol/format/Polyline.js';
  var Point = ol.geom.Point; //import Point from 'ol/geom/Point.js';
  var {Tile, Vector} = ol.layer; //import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
  var TileLayer = Tile;
  var VectorLayer = Vector;

  //var BingMaps = ol.source.BingMaps; //import BingMaps from 'ol/source/BingMaps.js';

  var VectorSource = ol.source.Vector; //import VectorSource from 'ol/source/Vector.js';
  var {Circle, Fill, Icon, Stroke, Style} = ol.style; //import {Circle as CircleStyle, Fill, Icon, Stroke, Style} from 'ol/style.js';
  var CircleStyle = Circle;

  // This long string is placed here due to jsFiddle limitations.
  // It is usually loaded with AJAX.
  var polyline = [ ...

如果要使用 ESRI sattelite mapOpenStreetMap(普通)地图而不是 Bing Maps 地图(需要密钥),请对标记动画示例进行此额外编辑:

  var map = new ol.Map({ //don't do new Map(...) here, uses some internal JS Map object in newer browsers
    target: document.getElementById('map'),
    loadTilesWhileAnimating: true,
    view: new View({
      center: center,
      zoom: 10,
      minZoom: 2,
      maxZoom: 19
    }),
    layers: [
      new TileLayer({
        source:
            //new ol.source.OSM()
        
            new ol.source.XYZ({
              attributions: ['Powered by Esri',
                             'Source: Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community'],
              attributionsCollapsible: false,
              url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
              maxZoom: 23
            })

            /*
            new BingMaps({
              imagerySet: 'AerialWithLabels',
              key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here'
            })
            */
      }),
      vectorLayer
    ]
  });

【讨论】:

  • vectorLayer - 你是如何创建(新)这个对象的?
  • var vectorLayer = new VectorLayer({ source: new VectorSource({ features: [routeFeature, geoMarker, startMarker, endMarker] }), style: function(feature) { /*如果动画处于活动状态则隐藏geoMarker */ if (animating && feature.get('type') === 'geoMarker') { return null; } return styles[feature.get('type')]; } });
  • 当心我在那里使用的“新地图”,它似乎不适用于内置地图 (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) 对象的最新浏览器。您需要改为 map = new ol.Map 并在开始时删除 var Map = ol.Map (将更新示例)
  • 再次感谢您对地图对象的提示。
【解决方案4】:
该示例不起作用,因为您可能只是从页面复制过去的数据而不查看 DOC 菜单项 https://openlayers.org/en/latest/doc/tutorials/bundle.html 所以一步一步: 1. npm init(在这里你将你的 index.js 设置为入口点) 2. npm install ol(它将 OpenLayer 添加到您的应用程序中) 3. npm install --save-dev parcel-bundler 4. npm start(它在 localhost:1234 上运行一个 Web 服务器,您将看到您的示例运行良好 如果需要,那么 5. npm build(它创建 /dist 文件夹,您需要将其复制到您的 Web 服务器,其中将转换为 js 文件。

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 2022-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2013-12-06
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多