Google Static Maps API key issues

I’ve just starting playing around with the Google Static Maps API as a complement to the regular Google Maps API, which as you probably know, is for JavaScript. The Static Maps API, on the other hand, provides a way to display static map images. This is useful in situations where you just need a non-interactive map and don’t want the overhead of an Ajax/JavaScript-based one.

However, I ran into some minor problems related to the API key usage with the Static Maps. Basically, an API key is tied to a certain domain name so that Google can keep track of your site’s usage. This means that the API key is tied to the domain name of the web site where you’re using the Google Maps API.

As in most development situations, I maintain a production server (with a real domain name) and a test/sandbox/development machine, which is just running off of my localhost. Thus, I had to get two API keys – one for the real domain name, and one for localhost and then switch between the two depending on the host name. (In PHP it’s fairly easy to check, using something like $_SERVER['SERVER_NAME'])

However, with the regular JavaScript-based Google Maps, it appears that if you’re running off of localhost (not 127.0.0.1) the service doesn’t check if you have a valid API key – this was probably done because many people are probably running test servers off of localhost and trying enforce usage limits would be counter-productive. (You do, however, need to have a valid API key – even if it doesn’t match localhost – if you want to use GClientGeocoder to resolve locations)

Because of this, at one point or another, I decided just to stick with using the production API key in all situations; after all, things continued to work even on the development server and in general, I don’t like making changes just for testing purposes.

This ended up causing problems when I started using the Static Maps API though. It turns out that this API does require you to have a valid API key matched to the host name in all circumstances, even if it’s localhost. This, of course, is the proper behaviour, but it differs somewhat from the behaviour of the standards Google Maps API.

Guess it pays to be correct in all situations, even if you don’t need to be!

Comments for this entry are closed

But feel free to indulge in some introspective thought.