我正在创建一个供自己使用的应用程序。此应用程序将在本地机器的电子表格中获取一列地址,并进行ajax调用以将其转换为纬度和经度。我现在遇到的问题是,它不是翻译所有地址,而是只翻译几个地址,每次我重新加载页面并检查时,计数都会有所不同。有些在通过 ajax 请求时返回空值。但是当我手动执行此操作时,它确实可以正确翻译。我这样做是否正确,还是有更好的方法
.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/>");
}
}
}
});
});
});
翻译成纬度和纬度
$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);
我在node中制作了一个脚本.js并让它运行了300次。如果失败,我记录了 JSON 数据。这是我在失败消息中得到的内容。
{ error_message: 'You have exceeded your rate-limit for this API.',
results: [],
status: 'OVER_QUERY_LIMIT' }
看起来你没事:)