Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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 - Fatal编程技术网

Javascript-变量范围

Javascript-变量范围,javascript,Javascript,我已经定义了名为“delka”和“sirka”的变量,我想在下面的函数中更改它们的值。显然,我做错了什么,因为当函数结束时,这些变量不受它的影响。为什么?谢谢你的回答 var sirka; var delka; var mestoNaLL = document.getElementById("mesto").value; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { "address": mesto

我已经定义了名为“delka”和“sirka”的变量,我想在下面的函数中更改它们的值。显然,我做错了什么,因为当函数结束时,这些变量不受它的影响。为什么?谢谢你的回答

var sirka;
var delka;
var mestoNaLL = document.getElementById("mesto").value;
var geocoder =  new google.maps.Geocoder();
        geocoder.geocode( { "address": mestoNaLL }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                sirka = results[0].geometry.location.lat();
                delka = results[0].geometry.location.lng();         
            } else {
                alert("Chyba: " + status);
            }
        });

        //undefined, why?
        alert(mestoNaLL + " " + sirka + " " + delka + " ");
编辑

这是同样的问题,对吗

//works fine
alert(markers[index].title + " " + infoWindows[index].content);

                    markers[index].addListener("click", function() {

                        //error - undefined
                        alert(markers[index].title + " " + infoWindows[index].content);

                        infoWindows[index].open(map, markers[index]);
                        map.setZoom(14);
                        map.setCenter(markers[index].getPosition());            
                    });
下面的代码是一个示例。这意味着,它不像其他任何东西一样被执行。在执行该代码之前,将执行您的警报,从而为您提供未定义的警告

geocoder.geocode( { "address": mestoNaLL }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        sirka = results[0].geometry.location.lat();
        delka = results[0].geometry.location.lng();         
    } else {
        alert("Chyba: " + status);
    }
});
解决方案是在
OK
中获取
警报

geocoder.geocode( { "address": mestoNaLL }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        sirka = results[0].geometry.location.lat();
        delka = results[0].geometry.location.lng();         
        alert(mestoNaLL + " " + sirka + " " + delka + " ");
    } else {
        alert("Chyba: " + status);
    }
});

因为geocode方法正在做异步的事情:谢谢,但是如果我需要变量从该函数中获取值,因为我以后必须使用它们?我真的不在乎警报,它只是测试消息。@Tom这是不可能的,你需要更改代码的工作方式。您需要将代码分解为多个部分。第一部分调用地理代码,第二部分在响应返回时运行。抱歉。谢谢大家,我已经重写了metod,它现在可以工作了,但我遇到了我希望的最后一个问题:我通过他们的API在google地图上绘制标记,标记的数据基于JSON回复。一切正常,但当我为标记注册onlick事件时,它总是为最后创建的标记打开信息窗口,而与我实际单击哪个标记无关。你知道我点击华盛顿,为华盛顿打开信息,但当我点击其他任何东西,比如说伦敦,它再次为华盛顿打开信息。我不明白为什么。