troycdunn
Expert
Gunning for gold!
Member Likes (6)
Contribution Note: WPMU Developer, Arnold, did basically all of this. I just wrote up instructions so that everyone else could do it on their website too.
This article covers how to add a Google Map to your single-listing.php template, dynamically creating a map for each listing based on Custom Press fields. Each user of your website could also add a map to their listing using the WPMU Google Maps Plugin, but this standardizes the look for a directory.
If you’re just starting with manually styling the Directory Plugin, start here.
For help with the primary content area and featured image, go here.
If you’re trying to add a Google Map to your directory listings, start by downloading the MapPress Easy Google Maps Plugin from Chris Richardson - http://wordpress.org/extend/plugins/mappress-google-maps-for-wordpress/
Activate the plugin for your site or for your network, depending on your needs. Mine is network activated.
(WPMU Google Maps Plugin should work with MapPress Easy Google Maps. It worked in my test using WP 3.3.2, WPMU Google Maps 2.5.3, and MapPress Easy Google Maps 2.38)
Under your site’s Settings > MapPress :
- Mark “Automatic map display” as “No automatic display”
- For “Post types” make sure “directory_listing” is checked
- Save
For additional MapPress info and screenshots, go here - http://wphostreviews.com/mappress-documentation
(Note: I’m currently using the free version but the pro version ($49.95 for unlimited sites) unlocks the “Custom Fields” settings, which may allow you to avoid editing your single-listing.php.)
Adding the Map to Your single-listing.php (listing template) Page
Open your single-listing.php page in a code editor. If you’re using the same code from my first and second post, locate this code:
<div id="rightColumn">
<!-- This is my right column -->
</div>
Add this code:
<div class="googleMap">
<?php
// Create Address from custom fields
$maddress = (array) do_shortcode('[ct id="ct_Street_Add_text_2a03"]');
$maddress[] = do_shortcode('[ct id="ct_City_text_407c"]');
$maddress[] = do_shortcode('[ct id="ct_StateProvi_text_e0af"]');
$maddress = implode(', ',$maddress) . ' ' . do_shortcode('[ct id="ct_ZipPostal__text_52b7"]');
//Create new map
$map = new Mappress_Map(array('width' => 632, 'height' => 200));
//Create pointer and locate from geocoding
$poi = new Mappress_Poi(array('address' => $maddress ));
$poi->geocode();
//Add the pointers
$map->pois = array($poi);
// Display it
echo $map->display(array() );
echo $maddress;
?>
</div>
Not a lot of styling is required to make this look nice. Here’s the CSS style, listed in my style.css file:
.googleMap {
float:left;
width:632px;
padding: 0;
margin: 10px 0 20px 0;
}
This floats the “googleMap” div to the left, sets a width to 632 pixels, removes any padding, and adds a margin of 10 pixels to the top and 20 to the bottom.
What the Code Does
This code starts with a div, which has the class "googleMap" added to it. The rest of the code inside the div is PHP. Thankfully Arnold has commented his code for use. Let’s look at the first part.
// Create Address from custom fields
$maddress = (array) do_shortcode('[ct id="ct_Street_Add_text_2a03"]');
$maddress[] = do_shortcode('[ct id="ct_City_text_407c"]');
$maddress[] = do_shortcode('[ct id="ct_StateProvi_text_e0af"]');
$maddress = implode(', ',$maddress) . ' ' . do_shortcode('[ct id="ct_ZipPostal__text_52b7"]');
In this code the key part for you is the [ct id=”something”]. Each of these CT’s correlates to a Custom Press field shortcode. To create these fields go to Custom Press > Content Types > Custom Fields > Add Custom Field.
The four fields I created were Street Address, City, State/Province, and Zip/Postal. Each of these were single line text fields.
Once your fields are created, hover over the name and click the "Embed Code" link. Copy and paste the shortcode into your single-listing.php file. It should replace my shortcode. You should also delete the property="title | description | value" attribute from each shortcode.
Altogether now, this php code will rewrite the address into one line of text. This allows it to print out in order and for Google Maps to do the look up of the location.
//Create new map
$map = new Mappress_Map(array('width' => 632, 'height' => 200));
This code does just what the comment says, it creates a map. In my case the size of the map is 632 pixels wide and 200 pixels tall. Change these numbers to resize your map.
//Create pointer and locate from geocoding
$poi = new Mappress_Poi(array('address' => $maddress ));
$poi->geocode();
This code again does what the comment says, creating a pointer on the map. It figure out where to place the pointer, based on the address entered into the Custom Press fields and combined together in the // Create Address from custom fields code.
//Add the pointers
$map->pois = array($poi);
// Display it
echo $map->display(array() );
echo $maddress;
And finally, the above code adds the created pointer to the map and displays the map and address on your page.
Pretty simple really. Big thanks to Arnold for his help with this!
(Note: Google Maps doesn't always work for locating an address. If a business has recently moved, the address is written wrong, or is incorrect in Google's database, the map may not work.)

Responses (21)
Member (joined May 2011) Likes (0)
Here is a screen shot again of how this looks on my site.
WPMUDEV-ian (joined November 2011) Likes (0)
Amazing share, Troy!
Once again you have shared a great tut and I've bookmarked this page.
We always love to view what our members have achieved with our plugins :) I loved the screenshot and can't wait to see your site go live so as we can see all the amazing work you have done :)
Amazing Amazing Amazing ! :)
Cheers
PC
DEV Diva (joined February 2012) Likes (1)Liked by: 
Great share Troy! Bookmarked for future reference :)
WPMUDEV-ian (joined November 2011) Likes (0)
Ditto :)
Cheers,
PC
Member (joined May 2011) Likes (1)Liked by: 
Thanks for the positive feedback. I'm hoping this is helpful for people. I was working on this for weeks and wasn't making any progress. Couldn't have done it without Arnold.
Member (joined November 2011) Likes (0)
Also a great how 2! I'm working with all your how 2's just right now :)
Small question; I use 1 custom field for the address.
Should the code than be:
Because it isn't working right. The address is displayed below the map, but the maps keeps loading and loading. You can see it at http://www.gemeentehollandskroon.net/blog/listings/quality-contacts-b-v/
Hmm, noticed this is in Firefox, in IE the fields and buttons for the directions are visible. But also no map.
Thanks,
Age
Developer (joined January 2012) Likes (0)
I'm getting a javascript error message on oyur page
Member (joined November 2011) Likes (0)
Check, I disabled the WPMU Google Maps, now it's working.
So it's not compatible together.
Any idea about getting this to work using the WPMU plugin which I prefer?
Thanks,
Age
DEV Diva (joined February 2012) Likes (0)
Hi @jongag1
I know that most of our plugins should work in harmony together, but I will ping the Developer to see if we can get WPMU Google Maps and Directory to work together [edit: for you].
Talk soon again.
Gina
Member (joined May 2011) Likes (0)
Gina, I think the Google Maps and Directory plugins work together. It's the MapPress Easy Google Maps Plugin from Chris Richardson that may not work with your Google Maps plugin.
:)
Member (joined November 2011) Likes (0)
Well, I can't get Google Maps and Directory working properly together....
And indeed MapPress Easy and Google Maps had a conflict, but the new beta off MapPress is working together with Google Maps. The beta and Google Maps are now working next to each other.
Developer (joined January 2012) Likes (0)
The WPMU google Maps isn't suited for what Troy was doing because it makes the Map information and stores it with a post rather than being able to create the map on the fly from just an address. Mappress has the interface to do it from an address which is why I used it instead.
Loading both map plugins will most likely load two copies of the Google Map API javascript which can cause all sorts of problems. Directory works with WPMU Maps but you can't script it to a custom field.
Member (joined October 2010) Likes (0)
Is this still the case?
Will wpmudev maps support directory listings natively?
Is it on the dev schedule?
Thanks
Johnny
Developer (joined January 2012) Likes (0)
No it's not native. The instructions here are for MapPress which is better adapted to integrating for this purpose than WPUDEV Google Maps. Not likely to be built into Directory because of the widely vaying mix of fields tehy want to use and where they want to put them. It's not that tough to add the bit of code above to get it working.
Member (joined October 2010) Likes (0)
http://premium.wpmudev.org/forums/topic/wpmudev-google-maps-directory-integration
I'm happy to wait until integration is possible.
Thanks,
Johnny
Member (joined October 2010) Likes (0)
WPMUDEV maps do work with Directory posts if anyone is wondering.
http://premium.wpmudev.org/forums/topic/how-to-style-the-directory-listings
All part of the mission: One Map to Rule Them All
http://premium.wpmudev.org/forums/topic/one-map-to-rule-them-all
:)
Member (joined October 2012) Likes (0)
Hey guys, really appreciate this post... saved me hours :)
Member (joined May 2011) Likes (0)
Quick heads up -- The MapPress plugin was upgraded on 12/17/12 to version 2.39.2. The developer moved the GeoCoding feature out of the free version. If you use this plugin and don't have the paid version, your maps will stop working.
You have two options:
1. Buy the Pro version ($49 one-time fee for unlimited websites)
2. Find another plugin that allows for GeoCoding
I opted to buy the plugin, which I did from this page:
http://wphostreviews.com/product/mappress
Hope this help. :)
People Person (joined February 2012) Likes (0)
Thanks Troy, I think most everyone on this ticket is using our Google Maps plugin tho :)
Member (joined February 2013) Likes (0)
If anyone was still hoping to use this code but cannot get it to work due to the updated version of mappress, I've worked out how it can be done with the google iframe embed code. See below;
Obviously still change your [ct id=""] references according to your custom field embed codes created using CustomPress.
Works perfect for me, and no need for another map plugin.
Member (joined November 2011) Likes (0)
Hi,
I'm still using the latest Mappress Pro version together with Directory and it's working and looking great.
Unfortunately I couldn't get the WPMU Google maps plugin to work with Directory, but I'm more than satisfied with Mappress!
You can see it here
With thanks to troycdunn and Arnold!
Gr,
Age
Become a member