3

I created my own custom control for openlayers 3 as follows:

window.app = window.app || {};
var app = window.app;

/**
 * Control Creado para moverse por el mapa (quitar controles de dibujar...)
 */
app.Move = function(opt_options) {

  var options = opt_options || {};

  var button = document.createElement('button');
  button.innerHTML = '<i class="fa fa-hand-o-up"></i>';

  var this_ = this;

  function move_ (){
      console.log();
      map.getControls().forEach(function (control) {
          console.log(control, 'control', window.app.GetFeatureInfo(), 'gfi');
          if(control instanceof (window.app.GetFeatureInfo())) {
              control.activar(false);
          }
          else if(control instanceof (new app.Draw())){
              control.removeDraw();
              control.removeModify();
              control.activar(false);
          }
      });
  }

  button.addEventListener('click', move_, false);

  var element = document.createElement('div');
  element.setAttribute('data-toggle', 'left');
  element.setAttribute('title', 'Mover');
  element.setAttribute('data-content', 'Desplazarse por el mapa');
  element.className = 'move ol-unselectable ol-control';
  element.appendChild(button);

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

};
ol.inherits(app.Move, ol.control.Control);
map.addControl(new app.Move());

In this control I want to 'turn off' other custom constrols. What I have done is to get all controls in the map and look up for my specific control using instanceof method.

  map.getControls().forEach(function (control) {
      //console.log(control, 'control', window.app.GetFeatureInfo(), 'gfi');
      if(control instanceof (window.app.GetFeatureInfo())) {
          control.activar(false);
      }
      else if(control instanceof (new app.Draw())){
          control.removeDraw();
          control.removeModify();
          control.activar(false);
      }
  });

But i'm getting this error:

TypeError: invalid 'instanceof' operand window.app.GetFeatureInfo(...)

How can I get a valid instance of custom control to compare it with the same custom control setted in the map?

2

window.app.GetFeatureInfo() and new app.Draw() are calls for new constructors and not the constructors themselfes. Instead you should use control instanceof app.Move or control instanceof app.GetFeatureInfo. Check the fiddle of you code. I am just logging the controls. Here is also some info about the instanceof. Check the examples

var myString = new String(); myString instanceof String; // returns true

2

I finally found myself what's going on.

instanceof method espected a constructor and the right code would be:

control instanceof (new window.app.GetFeatureInfo()).constructor)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.