A simple embedded Google map with address fetching in Drupal
As a newbie to Drupal, implementing a simple embedded Google map is more than confusing. More than a dozen modules claim to offer mapping functionality, but I just couldn’t get any of them to do what I needed. So I just did it myself.
What do I seek to accomplish?
I want to set up a directory with adresses and when viewing those nodes, I’d like to have a small embedded map that pinpoints the address. Very basic, really.
Why didn’t I just use a module?
I tried to get the job done with a couple of modules (Location, GMap and the like) but either no map showed up, if it did, it failed to fetch the address data and I generally became frustrated with the complexity of these modules that unfortunately coincides with a lack of documentation.
One module that was easy to use and worked just out of the box was embed google maps field, which lets you create a CCK field that turns into an embedded map. The only drawback is that it does not fetch address data from other fields but the user has to enter the address again into the embedded map field, which does not seem very user friendly when the user just entered the same data into special “street”, “town” and “zip code” fields a few lines above.
Hijacking this module’s iframe tag from the HTML code, I set out myself to feed the iframe dynamically with the address data.
What did I start from?
I’m in a Drupal 6.13 environment (I suppose any 6.x will do) and my little solution utilises the following modules:
Dynamic Field lets you create CCK fields that can be filled with PHP code and thus dynamically fills the field in view mode with computed data.
I also already have a custom content type with the CCK fields “street” and “town”.
What did I do?
1) Add a CCK field to your content type under admin/content/node-type/yourcontentype/fields
2) Choose “Dynamic Field” from the field type droplist
3) In the text field titled “PHP-Code”, add this code:
$street = $node->field_street[0]['value'];
$town = $node->field_town[0]['value'];
return "";
The first two lines just fetch the values of the current node’s town and street fields into more handy variables. The rest returns the Google maps iframe I hijacked from the Emded Google Maps Field module and inserts street and town in the right places.
In my experience, street and town are sufficient for Google to pinpoint the right spot on the map, but following the pattern above, you could of course also add more fields, like country or zip code.
Also, feel free to tinker with the settings in the iframe tag, that’s just plain HTML and can probably also be extended with further settings.
4) Create a test node for your content type, fill the address fields with an existing address and see if it all works.
As a plus, it doesn’t even clutter the edit view with ugly code, because the dynamic field does not show up here.
Update: Changing the zoom level
To change the zoom level of the embedded map is not straightforward, so I add this hint.
In the iframe tag, just change the z-value, right here: …&ie=UTF8&z=16&output=embed…
The lower the value, the farther out you zoom.
Update: Turn the info balloon on/off
Lazyburners figured this out by himself in the comments, but I was also looking for this functionality and I guess some others might be, too. Just add &iwloc=near to your iframe src and the balloon should vanish.
Leave me a comment if it worked for you!
Category: Drupal, IT Stuff 6 comments »
Facebook
Xing
Last.fm
Picasa Web Album
August 23rd, 2009 at 10:25
I keep getting: Parse error: syntax error, unexpected T_STRING in /var/www/vifun/public_html/modules/dynamicfield/phptext.module(46) : eval()’d code on line 4
[Reply]
August 23rd, 2009 at 10:56
I forgot the code tag around my snippet, so your problem is possibly linked to overly stylish ‘ and ” that PHP can’t read.
Fixed that though, try copy-pasting the code snippet now.
[Reply]
August 31st, 2009 at 15:52
Is there any variable so that google’s white rounded box with the address details do not appear. I have to click the X on the upper right hand corner for it to disappear.
If I use the map dimensions that you use width=”425″ height=”350″ sometimes the X of the white rounded box is out of the picture.
[Reply]
August 31st, 2009 at 17:05
Actually, I figured out how to configure the URL so that the Info bubble does not appear on the embedded map.
You might want to add this to the tutorial
&iwloc=near[Reply]
basanta Reply:
August 31st, 2009 at 20:09
Hey Lazyburners, thanks a bunch for that input as I have been looking for this myself. Small maps are almost completely covered by that balloon, which ultimately defies the sense of having an embedded map.
[Reply]
September 1st, 2009 at 12:45
Thank You for the great tutorial!
Can you advise how can I show the map in the block (side) bar so that the content match displayed node?
Eg. “City A” is defined in node “story-A” and “City B” in “story-B”. I would like to show “City A” in the Block, when reading “story-A” and equally for “City B”. When city is not defined, block should be stay hidden.
[Reply]