谷歌地理位置没有采取所有ajax参数的地址

时间:2016-01-22 08:47:41

标签: javascript php jquery ajax google-maps

我正在创建一个供我自己使用的应用。此应用程序将在本地计算机的电子表格中使用一列地址,并进行ajax调用以将其转换为纬度和经度。我现在的问题是,它没有翻译所有地址,只有少数几个,每次重新加载页面和检查时计数都会有所不同。有些通过ajax请求时返回null值。但是,当我手动执行它时,它确实正确翻译。 我正确地做了这个,或者有更好的方式做

PHP

<?php
include 'excel_reader/excel_reader.php';     // include the class

// creates an object instance of the class, and read the excel file data
$excel = new PhpExcelReader;
$excel->read('test.xls');

// Excel file data is stored in $sheets property, an Array of worksheets
/*
The data is stored in 'cells' and the meta-data is stored in an array called 'cellsInfo'

Example (firt_sheet - index 0, second_sheet - index 1, ...):

$sheets[0]  -->  'cells'  -->  row --> column --> Interpreted value
         -->  'cellsInfo' --> row --> column --> 'type' (Can be 'date', 'number', or 'unknown')
                                            --> 'raw' (The raw data that Excel stores for that data cell)
*/

// this function creates and returns a HTML table with excel rows and columns data
// Parameter - array with excel worksheet data
function sheetData($sheet) {
  $re = '<table>';     // starts html table

  $x = 1;
  while($x <= $sheet['numRows']) {
    $re .= "<tr>\n";
    $y = 1;
    while($y <= $sheet['numCols']) {
      $cell = isset($sheet['cells'][$x][$y]) ? $sheet['cells'][$x][$y] : '';
      $re .= " <td class='sheet'>$cell</td>\n";  
      $y++;
    }  
    $re .= "</tr>\n";
    $x++;
  }

  return $re .'</table>';     // ends and returns the html table
}

$nr_sheets = count($excel->sheets);       // gets the number of sheets
$excel_data = '';              // to store the the html tables with data of each sheet

// traverses the number of sheets and sets html table with each sheet data in $excel_data
for($i=0; $i<$nr_sheets; $i++) {
  $excel_data .= '<h4>Sheet '. ($i + 1) .' (<em>'. $excel->boundsheets[$i]['name'] .'</em>)</h4>'. sheetData($excel->sheets[$i]) .'<br/>';  
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Example PHP Excel Reader</title>
<style type="text/css">
table {
 border-collapse: collapse;
}        
td {
 border: 1px solid black;
 padding: 0 0.5em;
}        
</style>
</head>
<body>

<?php
// displays tables with excel file data
echo $excel_data;
?>    
<div class='lat' style='background-color:#ff0;'></div>
<div class='lon' style='background-color:#0FF;'></div>
</body>
</html>

Ajax请求:

$(function(){

 $("td.sheet").each(function() {
    console.log($(this).text());
     var address = $(this).text();
     var data;
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "test.php?address="+address,
      data: data,
      success: function(data) {
        // console.log(data);
        for(var i=0; i< data.length; i++)
        {
            if($.trim(data[i].latitude)!= ""){
            $(".lat").append(data[i].latitude+" => location = "+data[i].location+"<br/>");
            }

            if($.trim(data[i].longitude)!= ""){
            $(".lon").append(data[i].longitude+" => location = "+data[i].location+"<br/>");
            }
        }
      }
    });
 });

});

翻译成lat和lon

$location = $_GET['address'];
//$location="Malaysia 11200";
/*..location coordinate starts*/
 $coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($location) . '&sensor=false');
    $coordinates = json_decode($coordinates);


    $lat = $coordinates->results[0]->geometry->location->lat;
    $lng = $coordinates->results[0]->geometry->location->lng;

$array = array();
array_push($array,array("latitude"=>$lat,"longitude"=>$lng,"location"=>$location));
echo json_encode($array);

1 个答案:

答案 0 :(得分:1)

我在node.js中创建了一个脚本并运行了300次。如果失败,我记录了JSON数据。这是我在失败的消息上得到的。

$ docker stats redis1 redis2
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
redis1              0.07%               796 KB / 64 MB        1.21%               788 B / 648 B       3.568 MB / 512 KB
redis2              0.07%               2.746 MB / 64 MB      4.29%               1.266 KB / 648 B    12.4 MB / 0 B

看起来你没事:)