【发布时间】:2015-09-26 10:30:54
【问题描述】:
我正在创建一个应用程序,允许用户上传 CSV 文件并从每一行读取地址并进行地理编码。
我最初尝试使用 google map API 来执行此操作。但经过一番研究,我发现 google map api 有很多限制。例如,您一天只能进行 2500 次查询,而且每次查询都需要延迟时间。
这是我目前写的代码:
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<table width="600">
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<tr>
<td width="20%">Select file</td>
<td width="80%"><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td>Submit</td>
<td><input type="submit" name="submit" /></td>
</tr>
</form>
</table>
<?php
if ( isset($_POST["submit"]) ) {
if ( isset($_FILES["file"])) {
//if there was an error uploading the file
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else {
//Print file details
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
$name = $_FILES['file']['name'];
$ext = strtolower(end(explode('.', $_FILES['file']['name'])));
$type = $_FILES['file']['type'];
$tmpName = $_FILES['file']['tmp_name'];
// check the file is a csv
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
$row = 1;
while((($data = fgetcsv($handle, 1000, ',')) !== FALSE) && $row!=10) {
// number of fields in the csv
$col_count = count($data);
$address=implode(",",$data);
$geocode=file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".$address."&sensor=false");
print_r($geocode);
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;
sleep(1);
print_r($address);
echo "------".$lat."----".$lng;
echo "<br/>";
$row++;
}
fclose($handle);
}
}
}
} else {
echo "No file selected <br />";
}
}
?>
我的问题是,我如何使用 Bing Maps API 做到这一点。
这是一个示例 CSV 文件:
https://drive.google.com/open?id=0Bx3FBqTEy_0MaHp1QVhiNkVTLU0
【问题讨论】:
标签: php google-maps csv bing-maps bing-api