简体   繁体   English

Openlayers - 尝试在 map 上创建叠加功能

[英]Openlayers - trying to create the Overlay feature on the map

I am struggling with creating the overlay on my map.我正在努力在我的 map 上创建叠加层。 I want to incorporate the example like here:我想在此处合并示例:

https://openlayers.org/en/latest/examples/overlay.html https://openlayers.org/en/latest/examples/overlay.html

https://openlayers.org/en/latest/apidoc/module-ol_Overlay-Overlay.html https://openlayers.org/en/latest/apidoc/module-ol_Overlay-Overlay.html

but I cannot use the import statement, because I am getting an error:但我不能使用import语句,因为我收到一个错误:

Uncaught SyntaxError: Cannot use import statement outside a module未捕获的语法错误:无法在模块外使用 import 语句

which has an explanation here:这里有一个解释:

https://github.com/TypeStrong/ts-node#syntaxerror https://github.com/TypeStrong/ts-node#syntaxerror

and here:和这里:

Why examples don't work? 为什么示例不起作用? (a struggle with imports) (与进口的斗争)

"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 导入 ECMAScript 6 时出现“未捕获的语法错误:无法在模块外使用 import 语句”

I tried to do sth like this:我试图这样做:

      <script type="module" src="./layers/overlay.js" type="text/javascript"></script>

but an error still comes out and now it's related to the CORS policy:但仍然出现错误,现在它与 CORS 政策有关:

Access to script at 'file:///C:/Users.../layers/overlay.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https. CORS 策略已阻止从源“null”访问“file:///C:/Users.../layers/overlay.js”处的脚本:跨源请求仅支持协议方案:http,数据,镀铬,镀铬扩展,镀铬不受信任,https。

Unfortunately I need this feature offline.不幸的是,我需要离线使用此功能。

In this thread I found, that there is an alternative to the import feature:在这个线程中我发现,有一个替代导入功能:

https://gis.stackexchange.com/questions/310482/unexpected-token-identifier-error-import-openlayers/310501#310501 https://gis.stackexchange.com/questions/310482/unexpected-token-identifier-error-import-openlayers/310501#310501

and I tried to adjust my code into it, which looks like this:我尝试将我的代码调整为它,如下所示:

   var fromLonLat = ol.proj.fromLonLat

   var pos = fromLonLat([-0.21005,52.08093]);


   var overlay = new ol.Overlay({
   element: container,
   autoPan: true,
   autoPanAnimation: {
   duration: 250,
   },
  });

   var popup = new overlay({
   element: document.getElementById('popup'),
  });
   map.addOverlay(popup);

  // Vienna marker
  var marker = new overlay({
  position: pos,
  positioning: 'center-center',
  element: document.getElementById('marker'),
  stopEvent: false,
  });
  map.addOverlay(marker);

 // Vienna label
 var vienna = new overlay({
 position: pos,
 element: document.getElementById('vienna'),
 });
 map.addOverlay(vienna);

 map.on('click', function (evt) {
 var element = popup.getElement();
 var coordinate = evt.coordinate;
 var hdms = toStringHDMS(toLonLat(coordinate));

  $(element).popover('dispose');
  popup.setPosition(coordinate);
  $(element).popover({
  container: element,
  placement: 'top',
  animation: false,
  html: true,
  content: '<p>The location you clicked was:</p><code>' + hdms + '</code>',
  });
  $(element).popover('show');
   });

and now I am getting an error like this:现在我收到这样的错误:

Uncaught TypeError: overlay is not a constructor at overlay.js:15未捕获的 TypeError:overlay 不是 overlay.js:15 的构造函数

similar to the issue here:类似于这里的问题:

openlayers3 undefined is not a constructor error on ol.source.StaticVector openlayers3 undefined 不是 ol.source.StaticVector 上的构造函数错误

Regarding this I found:关于这一点,我发现:

https://github.com/Viglino/ol-ext https://github.com/Viglino/ol-ext

including all relevant extensions for OpenLayers.包括 OpenLayers 的所有相关扩展。 Unfortunately after attaching the relevant scripts, the problem is still the same.不幸的是,附加相关脚本后,问题仍然存在。

My another approaching was to replace everywhere the new overlay with the new ol.Overlay .我的另一种方法是用new ol.Overlay替换new overlay In this event the console says nothing, but I can't see an overlay at all.在这种情况下,控制台什么也没说,但我根本看不到覆盖。

The code might be specicif, because it comes from the QGIS2web plugin.该代码可能是特定的,因为它来自 QGIS2web 插件。 The major script with map as well as the index.html file you can find in this fiddle link below: map 以及 index.html 文件的主要脚本,您可以在下面的小提琴链接中找到:

https://jsfiddle.net/2adv41bs/ https://jsfiddle.net/2adv41bs/

Many sources refers me to the newest ol package许多来源将我引向最新的 ol package

https://openlayers.org/download/ https://openlayers.org/download/

but since I superseded the link in my HTML code it's still doesn't work at all但由于我取代了我的 HTML 代码中的链接,它仍然根本不起作用

I am also not familiar with creating the bundle in openlayers我也不熟悉在 openlayers 中创建包

https://openlayers.org/en/latest/doc/tutorials/bundle.html https://openlayers.org/en/latest/doc/tutorials/bundle.html

A similar thread is here:一个类似的线程在这里:

https://gis.stackexchange.com/questions/380382/incorporating-overlay-for-the-openlayers-map-generated-by-qgis2web-plugin https://gis.stackexchange.com/questions/380382/incorporating-overlay-for-the-openlayers-map-generated-by-qgis2web-plugin

Is it possible to launch the overlay option for Openlayers map offline?是否可以离线启动 Openlayers map 的覆盖选项?

The good alternative for import can be found here:可以在这里找到import的好选择:

https://gis.stackexchange.com/questions/310482/unexpected-token-identifier-error-import-openlayers/310501#310501 https://gis.stackexchange.com/questions/310482/unexpected-token-identifier-error-import-openlayers/310501#310501

which changes the situation completely, because now the final code can look as this:这完全改变了这种情况,因为现在最终的代码看起来像这样:

HTML HTML

   <div id="map" class="map">
        <div id="popup" class="ol-popup">
            <a href="#" id="popup-closer" class="ol-popup-closer"></a>
            <div id="popup-content"></div>
        </div>
    </div>
    <div style="display: none;">
    <!-- Clickable label for Vienna -->
    <a class="overlay" id="vienna" target="_blank" 
    href="http://en.wikipedia.org/wiki/Vienna">Vienna</a>
    <div id="marker" title="Marker"></div>
   
    <!-- Popup -->
    <div id="popup" title="Welcome to OpenLayers"></div>
   </div>

Next, our CSS接下来,我们的 CSS

   #marker {
    width: 20px;
    height: 20px;
    border: 1px solid #088;
    border-radius: 10px;
    background-color: #0FF;
    opacity: 0.5;
   }
    #vienna {
    text-decoration: none;
    color: white;
    font-size: 11pt;
    font-weight: bold;
    text-shadow: black 0.1em 0.1em 0.2em;
   }

   .popover-body {
     min-width: 276px;
   }

and the JS和JS

    var fromLonLat = ol.proj.fromLonLat

    var pos = fromLonLat([-0.19610,52.07769]);

    var popup = new ol.Overlay({
    element: document.getElementById('popup'),
    });
    map.addOverlay(popup);

    var marker = new ol.Overlay({
    position: pos,
    positioning: 'center-center',
    element: document.getElementById('marker'),
    stopEvent: false,
    });
    map.addOverlay(marker);

    var vienna = new ol.Overlay({
    position: pos,
    element: document.getElementById('vienna'),
    });
    map.addOverlay(vienna);

    map.on('click', function (evt) {
    var element = popup.getElement();
    var coordinate = evt.coordinate;
    var hdms = toStringHDMS(toLonLat(coordinate));

    $(element).popover('dispose');
    popup.setPosition(coordinate);
    $(element).popover({
    container: element,
    placement: 'top',
    animation: false,
    html: true,
    content: '<p>The location you clicked was:</p><code>' + hdms + '</code>',
    });
    $(element).popover('show');
    });

Obviously, both CSS and JS files must be attached to the main index.html file if we want it working.显然,如果我们希望它工作,CSS 和 JS 文件都必须附加到主index.html文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM