用于Google Maps API的OVER_QUERY_LIMIT - php到客户端

时间:2014-07-04 15:33:22

标签: php google-maps-api-3 limit geocoding 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.

?>

1 个答案:

答案 0 :(得分:0)

好的,我看了看并编译了这个......

<?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 ());
?>

就是这样......它就像一个魅力。

感谢您的指导。