【问题标题】:JavaScript getting variable from one function to anotherJavaScript 将变量从一个函数传递到另一个函数
【发布时间】:2012-01-17 06:28:09
【问题描述】:

我在将两个坐标从一个函数传递到另一个函数时遇到问题。我真的不知道 JavaScript,但它似乎有点正确。你能告诉我我的错误在哪里吗?

        <script type="text/javascript"> 

        function initialize() {
            var address = "Chicago IL";
            locs= getLatLong(address);

            alert(locs.lat()); //does not work
            var myOptions = {
                zoom: 4,
                center: new google.maps.LatLng(41, -87),
                mapTypeId: google.maps.MapTypeId.ROADMAP          
            };

            var map = new google.maps.Map(document.getElementById('map_canvas'),
            myOptions); 
        }

        function getLatLong(address){
            var geo = new google.maps.Geocoder;
            geo.geocode({'address':address},function(results, status){
                if (status == google.maps.GeocoderStatus.OK) {

                    locations[0] = results[0].geometry.location.lat(); 
                    locations[1] = results[0].geometry.location.lng();                  

                    //alert(locations[0]); //test is good- works! Need to pass it back to function
                    //alert(locations[1]); //test is good- works! Need to pass it back to function
                    locs =results[0].geometry.location; 
                    return locs;                
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    </script>

【问题讨论】:

标签: javascript function variables


【解决方案1】:

无法从异步函数返回值。不要尝试。

改为使用回调。

function setLocationOnMap(locs) {
    alert(locs.lat()); // works now!
    var myOptions = {
        zoom: 4,
        center: new google.maps.LatLng(41, -87),
        mapTypeId: google.maps.MapTypeId.ROADMAP          
    };
    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 
}

function initialize() {
    var address = "Chicago IL";
    getLatLong(address, setLocationOnMap);
}

function getLatLong(address, callback){
    var geo = new google.maps.Geocoder;
    geo.geocode({'address':address},function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            // processing...
            locs = results[0].geometry.location; 

            callback(locs);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

您必须了解,像 geo.geocode() 这样的异步函数调用会立即返回,即在任何结果准备好之前。这就是为什么你不能使用 return 从他们那里返回一个值——他们还没有。

一旦结果(通常是 HTTP 请求)准备就绪,异步函数就会依赖回调函数来处理它。您必须在该回调函数中直接或通过调用另一个函数来完成所有进一步的处理。

【讨论】:

  • 伙计们,我很困惑。我需要在这张地图上添加一些标记,它需要进入 for 循环并确认其他城市的其他位置。我怎样才能通过回调获得它们?抱歉,我了解传递对象和变量,但在 $ var position = new google.maps.LatLng(locs.lat(), locs.lng()); 之前从未遇到过回调概念var marker = new google.maps.Marker({ position: position, map: map }); marker.setTitle("芝加哥,伊利诺伊州");
  • 要设置 N 个标记,您需要向 geocode() 发出 N 个请求,所以这就是您的循环。 geocode() 函数已经接受了回调(注意您作为参数传递的 function()?)。只需使用此回调设置您的标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多