Search code examples
google-mapsgoogle-maps-api-3google-geocoding-api

Why do I receive OVER_QUERY_LIMIT answer of my first request?


I'm using the following code to get coordinates of a city, based on a request made by an Android App to my server.:

function getGPSLocation($reqLocation) {

    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $reqLocation . "&sensor=false";
    $attempt = 0;

    $this->log($url, 'debug' );

    $location = [
        'lat' => 0.0,
        'lng' => 0.0
    ];

    while ($attempt < 3) {
        $timeout = 5;
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        $content = curl_exec($ch);
        $resultHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);

        if ($resultHttpCode == '200') {
            $result = json_decode($content);

            if ($result->status == 'OVER_QUERY_LIMIT') {

                $this->log($result->status, 'debug' );

                sleep(2);
                $attempt ++;

            } else {

                /*
                 * TODO: Store the result to prevent other similar request later.
                 */
                if ($result != null) {
                    $location = $result->results[0]->geometry->location;
                }
                break;
            }
        }
    }
    return $location;

}

This code perfectly works on my local server but I get the OVER_QUERY_LIMIT error on any request when performed from my online server.

The Google doc (https://developers.google.com/maps/documentation/business/articles/usage_limits), doesn't tell me anything supposed to explain this behaviour.

Any idea?


Solution

  • Use a key in your request to the web service, so google can keep your requests separate from any others from that (I assume) shared server.

    from the documentation

    API Key

    Note: Maps for Work users must include client and signature parameters with their requests instead of a key.

    All Geocoding API applications should use an API key. Including a key in your request:

    Allows you to monitor your application's API usage in the Google Developers Console. Enables per-key instead of per-IP-address quota limits. Ensures that Google can contact you about your application if necessary. The Geocoding API uses an API key to identify your application. API keys are managed through the Google APIs console. To create your key:

    Visit the APIs console at Google Developers Console and log in with your Google Account. Click the Services link from the left-hand menu in the APIs Console, then activate the Geocoding API service. Once the service has been activated, your API key is available from the API > Access page, in the Simple API Access section. Geocoding API applications use the Key for server apps. To specify a key in your request, include it as the value of a key parameter.

    Note: By default, a key can be used from any server. We strongly recommend that you restrict the use of your key by IP address to servers that you administer. You can specify which IP addresses are allowed to use your API key by clicking the Edit allowed referers... link in the API console.

    Note: HTTPS is enforced for requests that include an API key.