• Home
  • Blog
  • Getting around Google Maps Geocoding API Lookup Limit

Getting around Google Maps Geocoding API Lookup Limit

The Google Maps V3 Geocoding API is handy little method for converting full or partial addresses in to geographic coordinates (latitude and longitude) or the other way round (Reverse Geocoding).

PHP Example:

[php]
<?php
header('Content-type: application/json');

$params = 'address='.urlencode('St Giles House, St Giles Street, Norwich, NR2 1JN, UK');

echo file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&'.$params);
?>
[/php]

When calling this API from a server using a language such as PHP a common pitfall or problem may occur due to the fairly low usage limits that are imposed on the API call, as described in Google's documentation:

Use of the Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day. (User of Google Maps API for Business may perform up to 100,000 requests per day.) This limit is enforced to prevent abuse and/or repurposing of the Geocoding API, and this limit may be changed in the future without notice. Additionally, we enforce a request rate limit to prevent abuse of the service. If you exceed the 24-hour limit or otherwise abuse the service, the Geocoding API may stop working for you temporarily. If you continue to exceed this limit, your access to the Geocoding API may be blocked.

Even if your application or website won't itself call 2,500 requests a day if you are hosted on a server that has other websites they may also be sending API requests - which all add up to the query limit as Google sees these requests coming from the same IP Address.

There are multiple ways round this, however one that has worked well for us here at Selesti is to move the logic for the Geocoding lookup from PHP to JS. By doing this we change who is sending the requests - from the server to the user.

To use an example, say you had to find the latitude and longitude coordinates for a user entering their address, you could do this lookup after the user has submitted the form and inside of PHP however you'll eventually face the query limit problem. What we need to do is to interject between the user submitting the form and the data being sent to the server.

To do this we'll prevent the form submission, hook in to it and do the address lookup. Below is some pseudo code to see how this would work:

[javascript]
var geocoder;

jQuery(document).ready(function() {

// Capture form submission
jQuery('#user_details').submit(function() {

var address = jQuery('#user_address').val();

// Perform lookup
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {

var location = results[0].geometry.location;

// Store this in hidden latlon input
jQuery('#user_latlon').val(location);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}

// Either way continue with form submission
jQuery('#user_details').unbind('submit').submit();
});

});

});
[/javascript]

As you can see, after preventing the form submission we send the Geocoding request and wait for the results, for a real client we would add in additional validation checks and loading screens to indicate to the user something is happening while they improve user experience.

Once we hear back from the Google API we check the result is ok and append the value to a hidden input in our form, the value should look something like 52.6291005,1.2847069.

Hopefully this have given you a decent idea of how to get round some of Google's query limitations and ensure you can continue to perform Geocoding lookups in the future. If you want to find out more about how the developers at Selesti can help you, why not get in touch?