-2

我试图让 OpenLayers 的功能只有在用户打开它的情况下才能工作。我制作了调用函数的 html 按钮,将布尔变量clickSearch变为 true 或 false。如何使该功能仅适用于clicksearch = true?它是某种 if 语句吗?

这是运行我要打开和关闭的功能的代码:

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                
            defaultHandlerOptions: {
                'single': true,
                'double': false,
                'pixelTolerance': 0,
                'stopSingle': false,
                'stopDouble': false
            },

            initialize: function(options) {
                this.handlerOptions = OpenLayers.Util.extend(
                    {}, this.defaultHandlerOptions
                );
                OpenLayers.Control.prototype.initialize.apply(
                    this, arguments
                ); 
                this.handler = new OpenLayers.Handler.Click(
                    this, {
                        'click': this.trigger
                    }, this.handlerOptions
                );
            }, 

            trigger: function(e) {
                var lonlat = map.getLonLatFromPixel(e.xy);
                alert("You clicked near " + lonlat.lat + " N, " +
                                          + lonlat.lon + " E");
            }
            });

clickSearch这些函数打开和关闭布尔值

var clickSearch= false;

function turnOn(){ clickSearch=true};
function turnOff(){ clickSearch=false};

这些按钮调用函数来打开clickSearch/关闭:

<button onclick="turnOn()">Turn on click-to-search</button>
<button onclick="turnOff()">Turn off click-to-search</button>
4

1 回答 1

2

当您希望它生效时,只需检查您的布尔值。例如,在您的触发函数中:

trigger: function(e) {
    if (!clickSearch)
        { return; }
    // ...
}
于 2013-07-12T13:05:18.863 回答