We are connecting to http://maps.googleapis.com/maps/api/geocode/xml using a PHP script to geocode a postcode into lon/lat. We are finding that we are constantly hitting the OVER_QUERY_LIMIT status. even though we are storing the postcodes for 1 day.
I would like to look at client side geocoding, but wasn't sure to do it exactly. please can someone take a look at this and see how best to re-write this so that we don't hit the overlimit.
Thanks
<?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.
?>
Ok, I had a look around and compiled this...
<?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"; }
?>
then I have the insert.php file which then inserts the postcode if it not in the database.
<?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());
}
?>
Elsewhere on the site, I clean up old postcodes if they are too old so as not to bloat my table.
<?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 ());
?>
That's about it... It works like a charm.
Thanks for the guidance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.