Google Maps in Drupal 7 - the simple way
For a client project last week, I needed to display automatically-generated embedded maps or links to external maps (e.g., a link to Google Maps), based on an entered address for a performance venue. In some display modes, I wanted a map on the page (such as when displaying a Venue page by itself), and in other display modes, I wanted a map link (such as when displaying venue information on a Performance page). Although this is, I'm sure, a common need on web sites, an extensive search for existing Drupal modules to didn't get me anywhere. The best solution I came up with for embedding the map involved at least three Drupal modules and several external library downloads, several of which were not yet released for Drupal 7 apart from "development" versions, and it didn't create the link to an external map that I needed.
So I thought to myself... This can't really be as difficult as using all these modules and libraries. When you go to Google Maps and map a location, Google provides code (click the Link icon near the map) for embedding that map in a web page in an iframe, and making a link to the map. I experimented a bit with the iframe and link code, and found that the latitude and longitude values could be omitted -- meaning that all you need is an address that Google maps can recognize.
This revelation allowed for a really simple solution. I used the Drupal 7 Field API to create a "Field Formatter" module that would take a plain text field containing an address that Google Maps could recognize, and instead of just displaying that text (which is what the default Text field formatter does), it would display as an embedded map, a link to Google Maps, or both. It took less than an hour to code, and works just fine for this project's purposes.
Update February 12, 2012: This module is now a full Drupal.org project, which you can download at: http://drupal.org/project/simple_gmap

Comments
Sweet!
Hi Jennifer,
I've been working on the IP Geolocation module and your idea of pushing a map into a field seems like a useful and simple extension for it.
Thanks for sharing.
Rik
Sounds good!
If you create something that replaces my simple formatter, post here again or contact me via drupal.org, and I'll put a link on my sandbox project page (and here).
That's exactly the module I
That's exactly the module I was looking for apart from taking the address from an Address Field rather than a standard text field.
That's a lot more complicated...
Taking the information from an Address Field is a lot more complicated, but you could probably take my module and adapt it. I consciously decided to make this one simple, thinking that it's not a huge amount of work for a content editor to type in a Google-recognizable address, even if it is a slight duplication of effort.
Full project now...
Just a note that this is now a "full" Drupal project rather than a sandbox, and it can be found at http://drupal.org/project/simple_gmap
Post new comment