Javascript ESRI';中的toolbar.deactivate()有问题;s API
我正在尝试创建一个按钮,单击该按钮将激活esri.toolbar.Draw.EXTENT,然后再次单击时将停用工具栏并返回正常地图导航 我所拥有的东西在第一次单击时起作用,但在第二次单击时似乎没有停用工具栏 除了工具栏外,其他一切都正常。deactivate()似乎没有启动Javascript ESRI';中的toolbar.deactivate()有问题;s API,javascript,draw,esri,arcgis-js-api,Javascript,Draw,Esri,Arcgis Js Api,我正在尝试创建一个按钮,单击该按钮将激活esri.toolbar.Draw.EXTENT,然后再次单击时将停用工具栏并返回正常地图导航 我所拥有的东西在第一次单击时起作用,但在第二次单击时似乎没有停用工具栏 除了工具栏外,其他一切都正常。deactivate()似乎没有启动 function initToolbar(map) { var currentvalue = document.getElementById('searchByExtent').value;
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>
函数初始化工具栏(映射){
var currentvalue=document.getElementById('searchByExtent').value;
var toolbar=新的esri.toolbars.Draw(映射);
如果(当前值=“关闭”){
document.getElementById(“searchByExtent”).value=“On”;
激活(esri.toolbars.Draw.EXTENT);
connect(工具栏,“onDrawEnd”,选择Stuff);
//toolbar.deactivate();
}否则{
document.getElementById(“searchByExtent”).value=“Off”;
toolbar.deactivate();
}
}
按范围搜索
您遇到了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.
相反,在函数外部定义工具栏
,以保持引用:
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();
}
}
您是否尝试过在}else{中放置一个console.log来确认它没有启动?这将是调试此程序的一个良好开端