javascript - 从地理编码循环外部访问数组

标签 javascript google-maps google-maps-api-3 geocoding

我已设法对多达 10 个地址进行地理编码并向 map 添加标记,但我需要在地理编码 (codeAddress) 函数之外访问地理编码结果。我想我可以将结果推送到一个全局变量数组(userLat、userLng)。使用警报我可以看到循环确实将结果添加到函数内的数组,但数组在函数外是没有值(value)的。

附带说明,不成功的地理编码警报应返回地址“i”,具体取决于哪个地址有问题,但仅显示循环的最后一个地址值。

也许问题是相关的?我想这可能与我对嵌套函数缺乏了解有关。

我希望这是清楚的。在此先感谢您的帮助!

   function codeAddress() {

    var useradd = [];
    var geocoder = new google.maps.Geocoder();
    var howmany = parseFloat(document.getElementById("howmany").value);

    for (var i=0; i<howmany; i++) {
        useradd[i] = document.getElementById('address['+i+']').value;
        geocoder.geocode( {address: useradd[i]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
                userLat.push(parseFloat(results[0].geometry.location.lat()));
                userLng.push(parseFloat(results[0].geometry.location.lng()));
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } 
            else {
                alert('Address ' + i + ' was not successfully located for the following reason: ' + status);
            }
        });
    };
}

编辑:

我不确定我是否理解正确,但已经提取了回调函数并创建了一个由地理编码器调用的新函数。问题仍然是一样的,变量 userLat 和 userLng 没有被带到循环之外。有什么建议吗?

函数代码地址() {

function callBack() {
    return function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
        userLat.push(parseFloat(results[0].geometry.location.lat()));
        userLng.push(parseFloat(results[0].geometry.location.lng()));
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
        } 
        else {
            alert('Address ' + i + ' was not successfully located for the following reason: ' + status);
        }
    }
}    

    var userLat = [];
            var userLng = [];
    var useradd = [];
    var geocoder = new google.maps.Geocoder();
    var howmany = parseFloat(document.getElementById("howmany").value);

    for (var i=0; i<howmany; i++) {
        useradd[i] = document.getElementById('address['+i+']').value;
        geocoder.geocode({address: useradd[i]}, callBack());
    };
}

最佳答案

当您的 codeAddress 函数返回时,geocoder.geocode 仍在执行其工作,异步geocoder.geocode 完成后,它将调用其回调,您将其作为匿名函数传递。

我建议的解决方案是使回调函数成为 codeAddress 的参数,并从该回调(或从回调内调用的其他函数)执行任何您需要的操作,而不是使用全局变量方法。

关于javascript - 从地理编码循环外部访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13604851/

相关文章:

javascript - JSON 解析错误 : Unterminated string

javascript - 任何让输入模式在 Safari 和 IE8 上工作的方法<

javascript - Google map 获取折线上的 LatLng

javascript - 无法访问 http 站点以在我的 https 站点中获取 map 标记

javascript - 在 IE 8 的 javascript 中启用/禁用下拉值

c# - 如何在 C# 中使用 GMAP.NET 在 map 上绘制圆圈

google-maps - 某个区域中的 Google map 自定义标记

火狐和谷歌地图 API v3 : IndexSizeError

javascript - 如何在 Google Maps API 搜索结果中显示自定义地点?

Javascript、原型(prototype)对象、jQuery 和计时器