简体   繁体   English

打字稿中的 openlayers 3 自定义控件

[英]openlayers 3 custom controls in typescript

The js code below adds custom controls to openlayers map下面js代码为openlayers地图添加自定义控件

/**
 * Define a namespace for the application.
 */
window.app = {};
var app = window.app;


//
// Define rotate to north control.
//



/**
 * @constructor
 * @extends {ol.control.Control}
 * @param {Object=} opt_options Control options.
 */
app.RotateNorthControl = function(opt_options) {

  var options = opt_options || {};

  var anchor = document.createElement('a');
  anchor.href = '#rotate-north';
  anchor.innerHTML = 'N';

  var this_ = this;
  var handleRotateNorth = function(e) {
    // prevent #rotate-north anchor from getting appended to the url
    e.preventDefault();
    this_.getMap().getView().setRotation(0);
  };

  anchor.addEventListener('click', handleRotateNorth, false);
  anchor.addEventListener('touchstart', handleRotateNorth, false);

  var element = document.createElement('div');
  element.className = 'rotate-north ol-unselectable';
  element.appendChild(anchor);

  ol.control.Control.call(this, {
    element: element,
    target: options.target
  });

};
ol.inherits(app.RotateNorthControl, ol.control.Control);


//
// Create map, giving it a rotate to north control.
//


var map = new ol.Map({
  controls: ol.control.defaults({
    attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
      collapsible: false
    })
  }).extend([
    new app.RotateNorthControl()
  ]),
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  renderer: exampleNS.getRendererFromQueryString(),
  target: 'map',
  view: new ol.View({
    center: [0, 0],
    zoom: 2,
    rotation: 1
  })
});

But I'm working on project using TypeScript instead javascript, and don't know how to make that work in typescript code.但是我正在使用 TypeScript 而不是 javascript 来处理项目,并且不知道如何在打字稿代码中使其工作。

Here's some part of code for openlayers map in typescript:以下是打字稿中 openlayers 地图的部分代码:

import openLayers = require('openLayers');

class OpenLayersMapProvider implements MapProvider {

    map: openLayers.Map;

    constructor(elementid: string, zoom: number = 8, tilesUrl?: string) {

            var RotateNorthControl = function (opt_options) {
            var options = opt_options || {};
            var anchor = document.createElement('a');
            anchor.href = '#rotate-north';
            anchor.innerHTML = 'BUTTON';

            var handleRotateNorth = function (e) {
                prevent #rotate-north anchor from getting appended to the url
                e.preventDefault();
               this_.getMap().getView().setRotation(0);
           };

            anchor.addEventListener('click', null, false);
        anchor.addEventListener('touchstart', null, false);

            var element = document.createElement('div');
            element.className = 'rotate-north ol-unselectable';
            element.appendChild(anchor);

            openLayers.control.Control.call(this, {
                element: element,
                target: this
            });


           //openLayers.inherits(RotateNorthControl(), openLayers.control.Control);




                layers = [new openLayers.layer.Tile({
                    source: new openLayers.source.XYZ({
                        url: tilesUrl + '/{z}/{y}/{x}'
                    })
                }),
                    this.vector, this.features]


            this.map = new openLayers.Map({
                target: elementid,
                controls: openLayers.control.defaults().extend([
                    new openLayers.control.FullScreen(),
                   , new RotateNorthControl(???)
                ]),
                interactions: openLayers.interaction.defaults().extend([this.select, this.modify]),
                layers: layers,
                view: new openLayers.View({
                    center: openLayers.proj.transform([9.66495667, 55.18794717], 'EPSG:4326', 'EPSG:3857'),
                    zoom: zoom
                })
            });

        source: openLayers.source.Vector;
        vector: openLayers.layer.Vector;
        features: openLayers.layer.Vector;


    }
    export = OpenLayersMapProvider;

Does anyone know what is equivalent of window.app in typescript?有谁知道打字稿中window.app 的等价物是什么? And how to do openLayers.inherits(RotateNorthControl(), openLayers.control.Control);以及如何做openLayers.inherits(RotateNorthControl(), openLayers.control.Control); ? ? I only know that I need add something to the openlayers.d.ts file.我只知道我需要在 openlayers.d.ts 文件中添加一些东西。

Thanx very much in advance for any help非常感谢您的帮助

A sample of custom control with typescript and ol3带有 typescript 和 ol3 的自定义控件示例

export class MyControl extends ol.control.Control {
    constructor() {
        super({});
        let button = document.createElement('button');
        button.type = 'button';
        button.className = 'ol-control';
        button.innerHTML = 'N';
        let element = document.createElement('div');
        element.className = 'ol-feature ol-control';
        element.appendChild(button);
        ol.control.Control.call(this, {
            element: element
        });
        button.addEventListener('click', () => this.click());
    }

    click() {
        console.log('click');
        console.log(this.getMap());
    }

}

Does anyone know what is equivalent if window.app in typescript?有谁知道如果 window.app 在打字稿中是等效的?

Hint : Define a namespace for the application.提示: Define a namespace for the application.

Typescript supports this with the concept of module . Typescript 通过module的概念支持这一点。 So所以

JavaScript: JavaScript:

window.app = {};
var app = window.app;
app.RotateNorthControl = function(opt_options) {

would be TypeScript:将是打字稿:

module app{
   export var RotateNorthControl = function(opt_options) {
   /// so on and so forth
}

how to do openLayers.inherits(RotateNorthControl(), openLayers.control.Control);怎么做 openLayers.inherits(RotateNorthControl(), openLayers.control.Control);

Use extends in TypeScript classes:在 TypeScript 类中使用extends

class RotateNorthControl extends openLayers.control.Control

You will need to rethink the purpose of RotateNorthControl from being a function to being a class.您需要重新考虑RotateNorthControl的目的,从函数到类。

Create a new class (as a separate TS-file) like so:创建一个新类(作为单独的 TS 文件),如下所示:

import { Control, defaults as defaultControls } from 'ol/control';
export class RotatControl extends Control {
    constructor() {
        super({});


        var button = document.createElement('button');
        button.innerHTML = 'N';

        var element = document.createElement('div');
        element.className = 'blahclass';
        element.appendChild(button);

        Control.call(this, {
            element: element
        });

        button.addEventListener('click', this.rotate.bind(this), false);
    }

    rotate() {
        super.getMap().getView().setRotation(0);
    };

}

Then import this control in your component - you dont't need t feed the constructor any arguments at all.然后在您的组件中导入此控件 - 您根本不需要为构造函数提供任何参数。

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

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