Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检测变量是否已在不同函数中更改_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

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,

我对javascript和谷歌地图还很陌生。我有一个输入,在提交时对值进行地理编码,我希望它:

  • 如果存在地图(已输入某些内容并成功进行地理编码 已经),然后重新居中
  • 否则,创建以该点为中心的地图
我的代码是这样的:

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)