javascript全局数组在地理编码后不维护新变量

时间:2016-08-31 09:31:10

标签: javascript google-api geocoding

我正在使用google api在网站中对地址进行地理编码。我们的想法是从数据库返回一个位置列表,进行地理编码,然后在地图上标记lat / lng。我正在使用的数据已经有lat / lng,但我将其归零以将其用作测试数据。

我遇到的问题是,地址成功地进行了地理编码并添加到位置数组的正确索引中。它们不在地理编码功能范围之外。

我的代码:

        var ajaxResult;//global variable

        function geocodeAddress(i)
        {
            var address = ajaxResult[i][2]+ajaxResult[i][3];

            geocoder.geocode({ 'address': address}, function(results, status)   
            {
                if(status === google.maps.GeocoderStatus.OK) 
                {
                    str = results[0].geometry.location;
                    str = str + '';
                    ajaxResult[i][6] = str.substring(1,str.indexOf(','));
                    ajaxResult[i][7] = str.substring(str.indexOf(',')+1,str.length-1);
                    alert(ajaxResult);
                }
                else
                {
                    alert(status);
                }
            }); 
        }

    /*
    CHECKS TO SEE IF THERE IS NUMERIC DATA IN THE LAT/LONG INDEXES
    */
        function checkCoordinates()
        {
            for(var i = 0; i < ajaxResult.length; i++)//loop to null out the lat/long
            {
                ajaxResult[i][6] = null;//test code
                ajaxResult[i][7] = null;//test code
            }
            alert(ajaxResult);
            for(var i = 0; i < ajaxResult.length; i++)
            {            
                if(ajaxResult[i][6] === 'undefined' || ajaxResult[i][6] === null || ajaxResult[i][7] === 'undefined' || 
                        ajaxResult[i][7] === null || !isNumeric(ajaxResult[i][6]) || !isNumeric(ajaxResult[i][7]))
                {
                    geocodeAddress(i);
                }
            }
        }

未触及的数据如下所示:

8397,St Roch's Tennis Courts, Valency Road ,GLEN IRIS,3146,,-37.85700607299805000000,145.05267333984375000000,,Sports,8395,St Andrew's Gardiner Tennis Club,21 Kyarra Road ,GLEN IRIS,3146,,-37.85311889648437500000,145.05355834960938000000,,Sports,8390,Penpraze Park,10 Victoria Road South ,EAST MALVERN,3145,,-37.87079620361328000000,145.03793334960938000000,,Sports,8388,Malvern Tennis Centre,43A Union Street ,ARMADALE,3143,,-37.85866165161133000000,145.02348327636720000000,,Sports,8387,Malvern Glendearg Tennis Club,256 Wattletree Road ,MALVERN,3144,,-37.86428833007812500000,145.03900146484375000000,,Sports

我有3个警告语句输出ajaxResult数组的状态。一个在地理编码循环之前(但是在使lat / lng归零之后),一个在期间和一个之后。

第一个警报输出:

enter image description here

for循环的最后一次迭代中的警报输出: enter image description here

最终提醒如下:

enter image description here

有谁能告诉我为什么我输入的纬度/经度值不能在创建它们的函数范围之外生存?

0 个答案:

没有答案