[英]OVER_QUERY_LIMIT for Google Maps API - php to client side
我們正在使用PHP腳本將http://maps.googleapis.com/maps/api/geocode/xml連接到lon / lat。 我們發現我們一直在達到OVER_QUERY_LIMIT狀態。 即使我們將郵政編碼存儲1天。
我想看一下客戶端的地理編碼,但不確定是否完全做到這一點。 請有人可以看看這個,看看如何最好地重寫它,這樣我們才不會超出上限。
謝謝
<?php
//database and connection are defined elsewhere
mysql_select_db($database, $connection);
//check if postcode exists in database
$sqlTS1 = mysql_query("SELECT * FROM `postcodestable` WHERE `postcode` = UPPER('" . $_GET['postcode'] . "') AND `date_added` > DATE_SUB(NOW(), INTERVAL 1 DAY)");
if(mysql_num_rows($sqlTS1)>0) {}
else
{
$postcodeTS1 = strtoupper($_GET['postcode']); // post code to look up
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcodeTS1."&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status=="OK")
{
//request returned completed time to get lat / lang for storage
$latTS1 = $xml->result->geometry->location->lat;
$longTS1 = $xml->result->geometry->location->lng;
}
//insert into postcodes table for server side caching of lon lat
$dateTS1= date('Y-m-d H:i:s');
$sqlinsert="INSERT INTO postcodestable (postcode,latitude,longitude, date_added) VALUES ('".$postcodeTS1."', '".$latTS1."', '".$longTS1."', '".$dateTS1."')";
$sqlinserting = mysql_query($sqlinsert) or die(mysql_error());
}
// we can now use $latTS1 and $longTS1 elsewhere within the HTML page
// we then run another script that removes all postcodes from Table where date_added is greater than 1 day.
?>
好吧,我環顧四周並編譯了這個...
<?php
mysql_select_db($database, $connection);
$sqlTS1 = mysql_query("SELECT * FROM `postcodestable` WHERE `postcode` = UPPER('" . $_GET['postcode'] . "')");
if(mysql_num_rows($sqlTS1)>0) {echo "postcode is already in database";}
else { $postcode1 = strtoupper($_GET['postcode']);
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false;key=MYAPIKEY"></script>
<script type="text/javascript">
<!--
function GetLocation() {
var geocoder = new google.maps.Geocoder();
var postcode = <?php echo json_encode($postcodeTS1); ?>;;
geocoder.geocode({ 'address': postcode }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
$.post("insert.php",
{ postcode1: postcode, lat1: latitude, long1: longitude },
function(data){
}
);
} else {
}
});
};
//-->
window.onload = GetLocation;
</script>
<?php
echo "postcode is not in database"; }
?>
然后我有insert.php文件,如果沒有在數據庫中,它將插入郵政編碼。
<?php
if (isset($_POST["postcode1"])) {
mysql_select_db($database, $connection);
$postcode2 = $_POST['postcode1'];
$lat2 = $_POST['lat1'];
$long2 = $_POST['long1'];
$date2= date('Y-m-d H:i:s');
$sqlinsert="INSERT INTO postcodestable (postcode, latitude, longitude, date_added) VALUES ('".$postcode2."', '".$lat2."', '".$long2."', '".$date2."')";
$sqlinserting = mysql_query($sqlinsert) or die(mysql_error());
}
?>
在網站的其他地方,我會清理舊的郵政編碼,如果它們太舊了,以免使我的桌子膨脹。
<?php
mysql_select_db($database, $connection);
$sql = "DELETE FROM postcodestable WHERE date_added < DATE_SUB(NOW(), INTERVAL 24 HOUR)";
$result = mysql_query($sql) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
?>
就是這樣...它就像一種魅力。
感謝您的指導。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.