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

Note: As with all modules on drupal.org, if you have comments or questions about this module, please search the issue queue to see if your question has already been answered, and if not, file a new issue. I'm closing comments on this post, meanwhile...

Tags:

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.

Google Map generated from Address Field

I had to write something similar but with Address Field and static maps, which led to http://drupal.org/project/addressfield_staticmap

I plan to add support for regular maps with the ability to degrade to the static image for mobile devices.

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

Thank you!

Great module - thanks so much! You've saved me quite a few hours of head-banging...

Thanks - works great! Do you

Thanks - works great!
Do you happen to know if there's an easy way to remove the panning controls and satellite and other map options?

Use the issue queue please...

If you have questions on this module, please check the module's issue queue and if your question is not answered there, add a new issue. (I think you already did this.) I try not to answer questions about modules on Drupal.org in multiple places, and the official place would be the issue queue. Thanks!

Gathering address info from a few fields

It seems simple and genial.

Let's consider a following situation, however. I have a big database in which there are addresses of the places specified in several independent text fields each (like there is a separate field for a city, a street, a postal code etc. -- user fills all of them or only some). I'd like to arrange values from these fields into one string of comma separated values in the order Google Maps can correctly recognize. That would be doable with your module if one could select "Google Maps from one line address" format next to a few text fields and then somehow sort their order. Do you think there is a simple way of getting that effect?

I don't think so...

That is certainly not something this module would handle.