0

我对谷歌的地理编码 api 有疑问:

我有一个地址,应该通过谷歌地理编码 api 使用 cURL 将其转换为纬度和经度。您将在下面看到我的代码。它工作得很好,但是突然停止工作,我得到了一个“OVER_QUERY_LIMIT”的答案。我查了一下,如果每天请求 api 超过 2500 次,通常会发生这种情况。这是不可能的,因为我的网站刚刚完成,每天大约有 20-40 个地理编码请求。

那么出现“OVER_QUERY_LIMIT”的真正问题是什么?我的代码有问题吗,谷歌以某种方式阻止了它?!

ini_set('allow_url_fopen', true);

$CookiePath = 'mycookiepath/geocookie.txt';
$userAgent = "mysite.com";
$ListURLRoot = "http://maps.googleapis.com/maps/api/geocode/json";
$ListURLSuffix = '&sensor=false';
$Curl_Obj = curl_init(); // Setup cURL
curl_setopt($Curl_Obj, CURLOPT_COOKIEJAR, $CookiePath);
curl_setopt($Curl_Obj, CURLOPT_USERAGENT, $userAgent);
curl_setopt($Curl_Obj, CURLOPT_HEADER, 0);
curl_setopt($Curl_Obj, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($Curl_Obj, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($Curl_Obj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl_Obj, CURLOPT_TIMEOUT, 30);
curl_setopt($Curl_Obj, CURLOPT_POST, 0); 



$stAddr = str_replace(' ','+', $ast);
$City = str_replace(' ','+', $ac);
$address = "$stAddr,+$City,+{$aco},+{$ap}";
$address = str_replace(' ', '+', $address);
$ListURL = "{$ListURLRoot}?address=$address$ListURLSuffix";
curl_setopt ($Curl_Obj, CURLOPT_URL, $ListURL);
$output = curl_exec ($Curl_Obj);
GetLocation($output);

function GetLocation($output)  {
    global $db_connect;
    $Loc = json_decode($output, true);
    if(isset($Loc))  {
        $i = 1;
        while ($Loc['status']=="OVER_QUERY_LIMIT") {
            if ($i > 14) {
                echo "Geocode failed!";
                exit();
            }
            sleep(1);
            $i++;

        }
        if(isset($Loc['status']) && stristr($Loc['status'], 'OK'))  {
            if(isset($Loc['results'][0]['geometry']['location']))  {
                $Lat = $Loc['results'][0]['geometry']['location']['lat'];
                $Lng = $Loc['results'][0]['geometry']['location']['lng'];
            }
        }
        else {
            error_log($Loc['status'], 0);
            echo "Unknown error occured!";
            exit();
        }
    }
}

提前致谢!

4

1 回答 1

1

主要是谷歌的问题。尽量避免服务器端地理编码。而是使用客户端 javascript 求解器。

于 2013-10-23T21:40:45.487 回答