此代码似乎完美无缺,但我每天不能超过2500个请求。我有成千上万的记录 连接
<?php
$servername = "localhost";
$username = "searchdb";
$passwd = "mypass";
$dbname = "mydb";
$test_table = "update_lon_lat";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $passwd); passwd);`enter code here`
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$i = 0;
$empty = "";
选择经度和纬度为空的所有记录
$stmt = $conn->prepare("SELECT client_id, address, city, state, zip FROM $test_table WHERE lat = :lat ");
$stmt->bindValue(':lat' , "$empty", PDO::PARAM_STR);
$stmt->execute();
如果为true,则计算新坐标并将其写入表
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$i = $i + 1;
$mapaddress = "";
$client_id = $row['client_id'];
$mapaddress = ($row['address'] . ' ' . $row['city'] . ' ' . $row['state'] . ' ' . $row['zip']);
下面开始讨论可疑代码
$address_holder = $mapaddress; // Google HQ
$prepAddr = str_replace(' ','+',$address_holder);
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat_holder = $output->results[0]->geometry->location->lat;
$longitude_holder = $output->results[0]->geometry->location->lng;
以上可疑代码的结尾
$stmt2 = $conn->prepare("UPDATE $test_table SET longitude = :longitude, lat = :lat WHERE client_id = :client_id");
$stmt2->bindValue(':longitude' , "$longitude_holder", PDO::PARAM_STR);
$stmt2->bindValue(':lat' , "$lat_holder", PDO::PARAM_STR);
$stmt2->bindValue(':client_id' , "$client_id", PDO::PARAM_STR);
$stmt2->execute();
echo $i . ' ' . $row['address'] . ' ' . $row['city'] . ', ' . $row['state'] . ' ' . $row['zip'] . ' lon=' . $longitude_holder
. ' Lat=' . $lat_holder . '<br>';
}
echo "Records Updated = " . $i;
?>code here
答案 0 :(得分:1)
选项1
您可以花一些钱来解锁更高的配额。
https://developers.google.com/maps/documentation/geocoding/usage-limits#premium-usage-limits
选项2
使用限制适用于每个项目。因此,创建多个项目,当您达到允许的最大限制时,只需将client_id更改为其他一些没有超过最大配额的项目。