简体   繁体   English

如何集成openlayers 3转换扩展

[英]How to integrate openlayers 3 transform extension

I am working on an openlayers 3 project, developed using typescript, hence: 我正在开发一个openlayers 3项目,使用typescript开发,因此:

let ol = require("openlayers");

I would like to use the transform extension plugin, which is not published on NPM ( http://viglino.github.io/ol3-ext/interaction/transforminteraction.js ) 我想使用转换扩展插件,它不在NPM上发布( http://viglino.github.io/ol3-ext/interaction/transforminteraction.js

I tried the following: 我尝试了以下方法:

let ol = require("openlayers");
require("<path>/ol/transforminteraction");

however I get the following errors: 但是我收到以下错误:

ERROR TypeError: ol.interaction.Transform is not a constructor 错误TypeError:ol.interaction.Transform不是构造函数

and

Uncaught ReferenceError: ol is not defined 未捕获的ReferenceError:ol未定义

How am I able include/integrate this resource correctly? 我如何才能正确包含/整合此资源?

let ol = require("openlayers");

This means ol is not in the global scope, therefore it isn't available to transforminteraction to extend. 这意味着ol不在全球范围内,因此无法通过转换来扩展。

Looking at the plugin code, you should be able to wrap it in a 看一下插件代码,你应该可以把它包装成一个

module.exports = function(ol) {
   ...
}

This puts ol into the function scope. 这将ol纳入功能范围。

Then you can use it by passing in ol as an argument to extend it. 然后你可以通过传入ol作为参数来扩展它来使用它。 eg 例如

let ol = require("openlayers");
require("<path>/ol/transforminteraction")(ol);

You can include "transforminteraction.js" code in a separate file that the browser will download alongside the HTML document. 您可以在浏览器将与HTML文档一起下载的单独文件中包含“transforminteraction.js”代码。 Cut JS code from html file,and paste it into your "transforminteraction.js" file,don't forget about window.onload = function() { ...your code js + transforminteraction.js } . 从html文件中剪切JS代码,并将其粘贴到您的“transforminteraction.js”文件中,不要忘记window.onload = function() { ...your code js + transforminteraction.js } Also check: 还检查:

Clone repository of OL3-ext: https://github.com/Viglino/ol3-ext 克隆OL3-ext的存储库: https//github.com/Viglino/ol3-ext

or: 要么:

https://github.com/Rep1ay/openLayer.git https://github.com/Rep1ay/openLayer.git

I solved this problem adding in my load.js file: 我解决了这个问题,在我的load.js文件中添加:

 if (debugMode) { addScriptFile('//cdnjs.cloudflare.com/ajax/libs/ol3/' + olVersion + '/ol-debug.js'); addScriptFile('//viglino.github.io/ol-ext/dist/ol-ext.js'); } else { addScriptFile('//cdnjs.cloudflare.com/ajax/libs/ol3/' + olVersion + '/ol.js'); addScriptFile('//viglino.github.io/ol-ext/dist/ol-ext.js'); } 

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

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