0

我正在尝试创建一个单击时将激活的按钮,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>
4

1 回答 1

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();
        }
    }
于 2014-10-14T00:08:57.743 回答