Displaying an address on a map (like with OpenLayers) can be nice, but sometimes the use of such a map is not wanted. As a midway I'd like to display a link to the address on Google Maps, Bing Maps, etc.
I'd like address field to have a setting for such a link, where you can optionally display a link to one or more maps services. It would be nice to have this link direct to the site in the user's preferred language as well.
For a Dutch website I'm working on, I quickly hacked this together. It's not the most representative code, but it does the trick in this case.
<?php
$address_render = render($content['field_event_address'][0]);
if ($address_render) {
$address = $content['field_event_address'][0]['#address'];
?>
<h3><?php print t('Location'); ?></h3>
<?php
print $address_render;
$location_string = str_replace(' ', '+', "{$address['thoroughfare']} {$address['postal_code']} {$address['locality']} {$address['country']}");
$url = "http://maps.google.nl/maps?q=$location_string";
$title = t('View on Google Maps');
printf('<br /><a href="%s">%s</a>', $url, $title);
unset($address_render, $address, $location_string, $url, $title);
}
?>
Comments
Comment #1
klucid commentedThanks for the code.
I'm a totally rookie at PHP and was wondering if you could help me out? I pasted this into a block with the PHP input filter enabled. Then I replaced your field name "field_event_address" with mine "field_address" and placed the block in the sidebar. Nothing is displayed.
Am I missing something?
Thanks in advance for you help!
Comment #2
MHLut commentedThis code is meant to be inside of a node template, where $content contains the values of the node's fields. Are you sure that $content in your block contains the right values? Also if you have loaded the address field through a view, you will have to use the $rows variable. I think this is where the problem comes from.
I also want to refer to the words "quickly hacked this together" above the code, the code works fine, but you may want to replace it when there's a nicer alternative.
Comment #3
rar commentedThanks MHLut
Two improvements:
1) For your code you lose the div that normally surrounds the address.
2) You shouldn't use printf for links when there is l() instead.
If you add divs for $address_render and the link to the map you get
where XXX is your machine_name for the content type (event in your example)
Comment #4
MHLut commentedThank you rar, I had changed l() in my code but didn't edit it here. I hadn't thought about the classes though, I am wondering why you're adding the divs, I haven't looked at the source yet, but shouldn't those be rendered using render($address)?
Comment #5
goron commentedMarked #1690536: Addressfield as Link as duplicate of this.
Comment #6
rszrama commentedI don't see us adding such a feature to this module - for starters, we'd have to continually expand the feature to support new mapping services, whereas the module is primarily concerned with entering / storing addresses. This sort of link is best suited to a contributed module that creates this link as an address field format plugin - perhaps with the form for it letting you specify the map service to use and the rendered version outputting the link as described above.
If anyone takes on that project, I'd be happy to link to it from the project page. Just lemme know if it happens.
Comment #7
dalinFor people that are looking for a quick-n-simple method rather than installing a new module (which doesn't seem to exist yet anyway), I'm marking this as a duplicate of #2528626, which while it won't get committed either, at least has a patch.