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.