Javascript 检测变量是否已在不同函数中更改
我对javascript和谷歌地图还很陌生。我有一个输入,在提交时对值进行地理编码,我希望它:Javascript 检测变量是否已在不同函数中更改,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我对javascript和谷歌地图还很陌生。我有一个输入,在提交时对值进行地理编码,我希望它: 如果存在地图(已输入某些内容并成功进行地理编码 已经),然后重新居中 否则,创建以该点为中心的地图 我的代码是这样的: var map; function getlatlong() { //this function gets the input value and then geocodes if(map.length){recentermap()} //if map exists,
- 如果存在地图(已输入某些内容并成功进行地理编码 已经),然后重新居中
- 否则,创建以该点为中心的地图
var map;
function getlatlong() { //this function gets the input value and then geocodes
if(map.length){recentermap()} //if map exists, recenter map
else{createmap()} //create map
}
function createmap(){ //this function creates the map (by editing `var map`)
var map = new google.maps.Map();
}
我想我只是不明白js变量是如何工作的。。。我的问题是,如果我在第二个函数中更改var map,我如何查看它是否已更改?当您说:
function createmap(){ //this function creates the map (by editing `var map`)
var map = new google.maps.Map();
}
您正在创建一个名为map的局部变量,该变量仅存在于函数createmap中。您希望使用在外部范围中声明的map
变量,因此不应使用var
关键字:
function createmap(){ //this function creates the map (by editing `var map`)
map = new google.maps.Map();
}
谢谢有没有GMAPSAPIV3的经验。。。如何检测是否已使用
var map
创建映射?如果仅使用全局map
而不是本地,map.length似乎不起作用。@并且您可以尝试使用if(map)
而不是if(map.length)