Google Geocoding API error message: “You have exceeded…” when trying to get latitude and longitude

When using the Geocoding API from apex code on Salesforce developer account, I get the following error:

{
   "error_message" : "You have exceeded your daily request quota for this API.",
   "results" : [],
   "status" : "OVER_QUERY_LIMIT"
}

I know that mean I exceeded my daily request, but the problem is it’s happens when I’m doing only few requests per day, it haven’t worked once.

I’m only trying a simple use of the API, so no way I’m exceeding my daily requests (which are 2,500 requests per 24 hour period).

Maybe I’m missing something…

I set up a remote site for: http://maps.googleapis.com

Here is my test page:

<apex:page controller="clsHandleGeoLocation">
    <apex:form >
        <apex:pageBlock title="input address">
            <apex:pageblockSection columns="3">
                <apex:inputText value="{!Address}" label="Address"/>
                <apex:commandButton value="Submit" reRender="locPanel" action="{!getGeoLocation}"/>
            </apex:pageblockSection>
        </apex:pageBlock>

        <apex:outputPanel id="locPanel">
            <apex:outputText value="Location: {!Location}">
            </apex:outputText>
        </apex:outputPanel>
    </apex:form>  
</apex:page>

Here is my controller code:

public with sharing class clsHandleGeoLocation 
{

    private static Integer RequestTimeout = 6000;

    public String Address {get; set;}
    public String Location {get; set;}

    public clsHandleGeoLocation() 
    {
        Address = '';
        Location = '';
    }

    public PageReference getGeoLocation()
    {
        system.debug('START getLocation');

        try
        {
            // Callout.
            Http httpGetRequest = new Http();
            HttpRequest httpReq = new HttpRequest();
            httpReq.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address=' + Address + '&sensor=false');
            httpReq.setMethod('GET');
            httpReq.setTimeout(RequestTimeout);
            HttpResponse httpResponseAddress = httpGetRequest.send(httpReq);

            system.debug('res body: ' + httpResponseAddress.getBody());
            JSONParser parser = JSON.createParser(httpResponseAddress.getBody());
            Double latitude = null;
            Double longitude = null;

            /* 
               "location" : {
               "lat" : 37.42291810,
               "lng" : -122.08542120
              } 
            */
            while (parser.nextToken() != null)
            {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)
                    && (parser.getText() == 'location'))
                    {
                        // Jump to latitude and longtitude fields.
                        parser.nextToken();

                        while (parser.nextToken() != JSONToken.END_OBJECT)
                        {
                            String text = parser.getText();
                            parser.nextToken();
                            if (text == 'lat') latitude = parser.getDoubleValue();
                            else if (text == 'lng') longitude = parser.getDoubleValue();
                        }
                    } 
            }

            // Update Account coordinates.
            if (latitude != null)
            {
                Location += latitude + ', ' +  longitude;               
            }

        }
        catch(Exception e)
        {
            system.debug('Error');
        }

        return null;
    }
}  

Best Regards.

Answer

As dphil pointed out, using Google Geocoding API in a shared environment won’t work.

Solution:

  1. Create an API Project: in Google Developer Console, just click
    on Create Project button.
  2. Assign Geocoding API to this project: click on ‘APIs & auth’ on the
    left menu, then search for Geocoding and click on the ‘On/Off’
    button.
  3. Assign a Key for server applications for the project: click on
    ‘Credentials’ on the left menu, then under ‘Public API access’ click
    on ‘Create New Key’, choose ‘Server key’.
  4. Copy the newly created key to the URL format, for example (note: when using key the url must be https):

    https://maps.googleapis.com/maps/api/geocode/json?key=MY_KEY&address=MY_ADDRESS&sensor=false

Attribution
Source : Link , Question Author : GoldenAxe , Answer Author : GoldenAxe

Leave a Comment