1-702-818-1043
support@quotaguard.com
Login

Accessing Google Maps Geocoding API from Heroku

Approximate time to read: 4 min


Why is my application broken? Geocoding was working fine locally but I’ve pushed it to Heroku and Google’s complaining I’m “OVER_QUERY_LIMIT”! Maybe I should have spent hours setting up EC2 after all…

Don’t! Heroku is great and you shouldn’t let a piddly thing like this lead to hours of sysadmin tasks.

Wait a minute…what is a Geocoding API?

The Google Maps Geocoding API is a great resource for small applications that want to include geocoded locations. Their free API currently allows 2,500 daily requests which is a healthy amount for most applications.

So you start developing your app locally, happily geocoding away until you think you’ve got a pretty nice product and are ready to launch. Being a lean developer who doesn’t want to get involved with sys admin tasks you do the right thing and get yourself a Heroku account and a few pushes later your app is running on Heroku but your geocoding has come crashing down as Google slaps down your usage limit.

Where did my usage limit go?

Google tracks the usage of its free APIs based solely on IP address. You can’t get an API key unless you sign-up for a Google Maps for Business account, a good choice for large businesses but frighteningly expensive for us independent devs (think thousands of dollars a year). The problem with Heroku and all other shared cloud hosting providers like Rackspace, CloudControl, OpenShift etc, is that your application traffic is routed out to Google via a shared IP. Google doesn’t know there are hundreds of applications behind this IP so sticks to its 2,500 limit.

This leads to a bit of a winner takes it all situation on Heroku: “First 2500 requests win a latitude and longitude, the rest of you get lost!”.

So your geocoding is now unreliable. Maybe you’ll get some coordinates, maybe you won’t? Not a recipe for a great product.

Enough already, we’re developers – how do we hack it?

Good news! There are many different ways to solve this problem, I’m biased as I’ve setup the awesome QuotaGuard but I’ll give you all the options:

1. Keep hammering at the door

Setup a job using Heroku scheduler (their cron equivalent) that periodically gets a list of all objects that still need geocoding and try and hit the API. If you’re like Daft Punk you might Get Lucky but you’re more likely to be left empty handed.

Pros

  • It might work?
  • Free

Cons

  • Extra coding for Geocoding job
  • Will take a long time to geocode all objects
  • Indeterminate when you will have completed geocoding

2. Shift to client-side geocoding

If you are geocoding objects based solely on user input you can use the Javascript API to geocode your results in the client’s browser. These requests will be made to Google using your user’s IP address so have a much better chance of being within the daily usage limit.

Javascript example:

geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
 map.setCenter(results[0].geometry.location);
 var marker = new google.maps.Marker({
 map: map,
 position: results[0].geometry.location
});

Once you have the required geocoding results in the browser you will have to post the data back to your server via a form or AJAX request.

Pros

  • High chance of successful geocoding
  • Free

Cons

  • Only works if client enters data to be geocoded
  • Javascript development required
  • Need backup strategy in case client geocoding fails

3. Setup your own proxy server

If you want to get your sys admin hands dirty then you can pick your favourite cloud hosting provider (EC2, Rackspace, Digital Ocean), create a new server instance, open up the correct ports and install your own proxy server. You can then configure your application to route Google Maps API requests through this proxy and you will get your 2,500 requests.

Pros

  • You control the proxy so guaranteed to get 2500 requests
  • No application code changes needed

Cons

  • You pay monthly fee for hosted instance
  • Extra infrastructure component to manage outside Heroku

4. Use QuotaGuard

We setup QuotaGuard after going through the above pain for our own application and thinking, this is not Heroku best practice.

You can think of QuotaGuard as a Heroku add-on specifically for proxying requests to IP limited APIs. Sign-up for a free trial and you will get a proxy address that you can use to route all API requests and you can forget about managing any infrastructure again.

user:pass@proxy.quotaguard.com:9292

We have a network of EC2 servers actively monitoring the number of requests sent to each API to make sure we never get the dreaded OVER_QUERY_LIMIT.

Pros

  • Guaranteed to get daily 2500 requests
  • No infrastructure knowledge needed
  • No ongoing maintenance of proxy server
  • No application code changes needed

Cons

  • You pay (low) monthly fee for service

So I should use QuotaGuard?

Of course! It’s scalable, low cost and means you don’t have to get stuck in to infrastructure, that’s why you chose Heroku in the first place right?

See what QuotaGuard is all about on our main site or sign up directly on the QuotaGuard Heroku add-ons page.


Ready to Get Started?

Get in touch or create a free trial account