在ESRI的API中遇到toolbar.deactivate()问题

时间:2014-10-12 22:20:03

标签: javascript draw esri arcgis-js-api

我正在尝试创建一个按钮,点击该按钮将激活 esri.toolbars.Draw.EXTENT ,然后再次单击将停用工具栏并返回到法线贴图导航。

第一次点击我的工作原理,但第二次点击似乎没有停用工具栏。

除了 toolbar.deactivate() 之外,其他所有内容似乎都有效

function initToolbar(map) {
            var currentvalue = document.getElementById('searchByExtent').value;
            var toolbar = new esri.toolbars.Draw(map);
            if (currentvalue == "Off"){
                document.getElementById("searchByExtent").value="On";
                toolbar.activate(esri.toolbars.Draw.EXTENT);
                dojo.connect(toolbar, "onDrawEnd", selectStuff);
                //toolbar.deactivate();
            } else {
                document.getElementById("searchByExtent").value="Off";
                toolbar.deactivate();
            }
        }

<input type    = "button"
       id      = "searchByExtent"
       value   = "Off"
       onclick = "initToolbar(map);">
       Search by Extent
     </input>

1 个答案:

答案 0 :(得分:1)

您有一个Javascript范围问题。

当您激活工具栏时,一切都运行良好:

var toolbar = new esri.toolbars.Draw(map);
...
toolbar.activate(esri.toolbars.Draw.EXTENT);

...并且您在名为toolbar的变量中有一个活动工具栏....但该变量是initToolbar函数的本地变量。该函数退出,变量丢失。当您尝试停用工具栏时,再次致电initToolbar

var toolbar = new esri.toolbars.Draw(map); // This is NOT the same toolbar!
...
toolbar.deactivate(); // Makes no sense, it's not active.

相反,在函数外部定义toolbar,以便维护引用:

var toolbar = null; // define it here

function initToolbar(map) {
        var currentvalue = document.getElementById('searchByExtent').value;
        if (currentvalue == "Off"){
            toolbar = new esri.toolbars.Draw(map); // Create the toolbar here

            document.getElementById("searchByExtent").value="On";
            toolbar.activate(esri.toolbars.Draw.EXTENT);
            dojo.connect(toolbar, "onDrawEnd", selectStuff);
        } else if (toolbar) { // If your value is not "Off" and the toolbar exists, then we can kill it.
            document.getElementById("searchByExtent").value="Off";
            toolbar.deactivate();
        }
    }