【问题标题】:OVER_QUERY_LIMIT for Google Maps API - php to client sideGoogle Maps API 的 OVER_QUERY_LIMIT - php 到客户端
【发布时间】:2014-08-25 23:33:12
【问题描述】:

我们正在使用 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.

?>

【问题讨论】:

  • 客户端通常意味着使用javascript来处理调用。因此,您需要在 html/js 而不是服务器端代码中添加请求
  • 谢谢。我将尝试搜索地理编码 javascript,然后将变量转换为 PHP。我对 php 很好,但不是 javascript
  • 这无法解决超出限制的问题。所有 PHP 都在服务器端执行。 JS 在客户端运行(在用户的浏览器中,而不是在服务器上运行)。 GMaps“知道”区别。
  • 从这里开始:developers.google.com/maps/documentation/javascript/geocoding 菜单项代码示例中提供了一些示例代码片段。 “阅读”与 PHP 并没有太大的不同,你可能会理解。
  • 所以方法是:用地理编码脚本嵌入JS文件然后在JS文件中返回lon lat然后将lon lat转换为php变量然后像往常一样使用php变量....

标签: php google-maps-api-3 limit geocoding client-side


【解决方案1】:

好的,我环顾四周并编译了这个...

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

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

感谢您的指导。

【讨论】:

    猜你喜欢
    • 2014-06-20
    • 2013-11-17
    • 2020-09-23
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2017-08-04
    相关资源
    最近更新 更多