Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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_Jquery_Geocoding - Fatal编程技术网

使用异步地理编码函数填充javascript数组

使用异步地理编码函数填充javascript数组,javascript,jquery,geocoding,Javascript,Jquery,Geocoding,因此,我正在解析信息并查询具有多个地址的地理代码。我不完全确定什么是最好的方法。这就是我要做的 for($j = 0; $j < $rawArray.length; $j++){ $baseStr = $addresNum != 'empty' ? $rawArray[$j][$addresNum] + ' ': ''; $baseStr += $addresStr != 'empty' ? $rawArray[$j][$addresStr] + ' ': '';

因此,我正在解析信息并查询具有多个地址的地理代码。我不完全确定什么是最好的方法。这就是我要做的

for($j = 0; $j < $rawArray.length; $j++){
    $baseStr = $addresNum != 'empty' ? $rawArray[$j][$addresNum] + ' ': '';
    $baseStr += $addresStr != 'empty' ? $rawArray[$j][$addresStr]  + ' ': '';
    $baseStr += $addresDiv != 'empty' ? ', ' + $rawArray[$j][$addresDiv] : '';

    $baseStr = ($baseStr.toLowerCase().indexOf("qc")  >= 0 || $baseStr.toLowerCase().match(/qu[e-é]bec/) ?  $baseStr : $baseStr + ", Qc");

    console.log("Looking for: " + $baseStr.match(/\w\d\w([ ])*\d\w\d/i)[0]);
    $baseStr = $baseStr.match(/\w\d\w([ ])*\d\w\d/i)[0];

    $geocoder.geocode({
        address: $baseStr
    }, function(locResult, status) {
        $arrayCoords[$j] = new Array();
        if (status == google.maps.GeocoderStatus.OK) {
            $arrayCoords[$j][0] = locResult[0].geometry.location.lat();
            $arrayCoords[$j][1] = locResult[0].geometry.location.lng();
        }else {
            $arrayCoords[$j][0] = '';
            $arrayCoords[$j][1] = '';
        }
    });
    console.log("found: " + $arrayCoords[$j][0] + $arrayCoords[$j][1]);
}
($j=0;$j<$rawArray.length;$j++)的
{
$baseStr=$addresNum!='empty'?$rawrarray[$j][$addresNum]+'':'';
$baseStr+=$addresStr!=“空”?$rawArray[$j][$addresStr]+'':“;
$baseStr+=$addresDiv!='empty'?','+$rawArray[$j][$addresDiv]:'';
$baseStr=($baseStr.toLowerCase().indexOf(“qc”)>=0 | |$baseStr.toLowerCase().match(/qu[e-é]bec/)?$baseStr:$baseStr+”,qc”);
log(“查找:”+$baseStr.match(/\w\d\w([])*\d\w\d/i)[0]);
$baseStr=$baseStr.match(/\w\d\w([])*\d\w\d/i)[0];
$geocoder.geocode({
地址:$baseStr
},函数(结果,状态){
$arrayCoords[$j]=新数组();
if(status==google.maps.GeocoderStatus.OK){
$arrayCoords[$j][0]=locResult[0].geometry.location.lat();
$arrayCoords[$j][1]=locResult[0].geometry.location.lng();
}否则{
$arrayCoords[$j][0]='';
$arrayCoords[$j][1]='';
}
});
log(“找到:+$arrayCoords[$j][0]+$arrayCoords[$j][1]);
}
现在,我认为填充一个数组并使用它将是一个好主意。所以我这样做了:

$timeout = setInterval(function() 
                    {
                        for($j = 0; $j < $rawArray.length; $j++){
                            console.log($globalGoogleArray[$j]);
                        }

                        if($globalGoogleArray.length == $rawArray.length)
                        {
                            console.log($globalGoogleArray);
                           //TODO: stopTimer();
                        }
                    }, 100);
$timeout=setInterval(函数()
{
对于($j=0;$j<$rawArray.length;$j++){
log($globalgoglearray[$j]);
}
if($globalgoglarray.length==$rawArray.length)
{
log($globalgoglarray);
//TODO:stopTimer();
}
}, 100);
就在
console.log之前(“发现:+$arrayCoords[$j][0]+$arrayCoords[$j][1])

我添加了
$globalgoglarray[$j]=$arrayCoords[$j][0]+,“+$arrayCoords[$j][1]

如果我可以用我的值填充
$globalgoglarray
,那么我可以停止计时器并触发一个函数,该函数将与数组的内容一起工作。这可能不是最好的方法,我愿意接受建议,但我不会以我想要的结束。计时器位于
for
的顶部,其中的console.log只返回
for
中的console.log(这一个:
console.log(“找到:+$arrayCoords[$j][0]+$arrayCoords[$j][1]);
)不会输出我希望它输出的内容


有人能告诉我为什么我不能在GlobalgoLearRay中获得地理代码的输出吗?

跟踪回调函数中返回的调用数。当最后一个返回时,处理
$arrayCorrds
数组

var resultCount = 0; //counter for number of calls that have returned

for($j = 0; $j < $rawArray.length; $j++){

    ...

    $geocoder.geocode({
        address: $baseStr
    }, function(locResult, status) {
        $arrayCoords[$j] = new Array();

        if (status == google.maps.GeocoderStatus.OK) {
            $arrayCoords[$j][0] = locResult[0].geometry.location.lat();
            $arrayCoords[$j][1] = locResult[0].geometry.location.lng();
        }else {
            $arrayCoords[$j][0] = '';
            $arrayCoords[$j][1] = '';
        }

        resultCount++; //increment count
        if(resultCount === ($rawArray.length - 1)) {
            //the last result has been retrieved, do something with $arrayCoords here
        }

    });
}
var resultCount=0//返回的呼叫数计数器
对于($j=0;$j<$rawArray.length;$j++){
...
$geocoder.geocode({
地址:$baseStr
},函数(结果,状态){
$arrayCoords[$j]=新数组();
if(status==google.maps.GeocoderStatus.OK){
$arrayCoords[$j][0]=locResult[0].geometry.location.lat();
$arrayCoords[$j][1]=locResult[0].geometry.location.lng();
}否则{
$arrayCoords[$j][0]='';
$arrayCoords[$j][1]='';
}
resultCount++;//递增计数
if(resultCount===($rawArray.length-1)){
//已检索到最后一个结果,请在此处使用$arrayCoords执行操作
}
});
}

所以,知识终于像一杯冷水一样打动了我。我只是想模拟一个循环

我最终创建了两个相互调用的函数。我也认为这就是我应该如何处理这种情况

函数1由具有空字符串的外部函数调用:

function collectCoords($fromGoogleStr){

    //If being called with params, add them
    if($fromGoogleStr)
        $globalGoogleArray[($globalGoogleArray? $globalGoogleArray.length : 0)] = $fromGoogleStr;

    //Generate address string here here\\


    if ($tmpCounter < $fullArray.length - 1){
        $tmpCounter++;
        generateGoogleCoords($adressString);
    }else{
    //Do something with the complete answer
    }
}
所以,答案并不是什么花哨的东西。当Google应答时,collect函数被再次调用,并且该函数中的if可以防止所有这些进入一个永无止境的循环

德国劳埃德船级社


阿克塞尔

是的,你说得对。这显然是我必须做的事情。但这不是一个解决方案。在您添加的­­­
if
中,我写了以下内容:
$globalgoglarray=$arrayCoords和之后的console.log。结果是,我得到了一堆具有正确坐标的“found”,然后是一个数组,其中除最后一个插槽外的所有插槽都未定义(最后一个插槽包含最后一对正确的坐标)
function generateGoogleCoords($baseStr){

    $geocoder = new google.maps.Geocoder();
    $arrayCoords = new Array();

    $geocoder.geocode({
        address: $baseStr
        }, function(locResult, status) {
            $arrayCoords[$j] = new Array();
            $arrayCoords[$j][0] = locResult[0].geometry.location.lat();
            $arrayCoords[$j][1] = locResult[0].geometry.location.lng();
            collectCoords($arrayCoords[$j][0] + ", " +$arrayCoords[$j][1]);
    });
}