HowTo: work around GoogleAPI single directory limit

The GoogleAPI, when publicly released as version 1, required the use of an API key that is assigned to a user on a per directory basis. What this means is that the particular GoogleMap API key can only be used in that particularly directory on a website and not any other directory. For example, a key generated for would not work for


This is particularly a problem when your website uses rewrites to make per-tee urls for posts instead of the ?p=141. Each post looks like a separate directory and therefore the sidebar with map you see to your right (if you're not viewing the RSS feed) doesn't work.

The work around for this is to place the GoogleMap in an iframe, which is an inline frame and acts as a window to another webpage. That webpage is just a GoogleMap that sits at the base directory of your website. Any other page can now include that GoogleMap by using a single API-key.

To illustrate, your webpage would include the following html where appropriate:

The corresponding googleMapFrame.php is just a wrapper around the GoogleMap call and code. This file needs to reside in the directory where you generated your API-key:


That's all you have to do. This work around actually has the nice effect of making adding a GoogleMap to the website very easy. I don't have to worry about including the initial GMap javascript include, or creating a div container and then calling the javascript code. I just put the iframe whereever I want it on the page and *poof!*, there it is.

It would also be a good idea to add the ability to set the size of the GoogleMap (and iframe) on a per page basis, and do multiple waypoints, tracks etc. Essentially a fully functional mini-api wrapper around the larger GoogleMap API.

About this article

written on
posted in ProgrammingMapsWeb Back to Top

About the Author

Andrew Turner is an advocate of open standards and open data. He is actively involved in many organizations developing and supporting open standards, including OpenStreetMap, Open Geospatial Consortium, Open Web Foundation, OSGeo, and the World Wide Web Consortium. He co-founded CrisisCommons, a community of volunteers that, in coordination with government agencies and disaster response groups, build technology tools to help people in need during and after a crisis such as an earthquake, tsunami, tornado, hurricane, flood, or wildfire.