Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active November 3, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/f3ecf9d748889d16b9ceff1efe2c07da to your computer and use it in GitHub Desktop.
Save xnau/f3ecf9d748889d16b9ceff1efe2c07da to your computer and use it in GitHub Desktop.
Demontrates a single record template for Participants Database that includes a location map for the listing
<?php
/*
* template for showing a google map in a single record template
*
*/
/*
* this is the name of the group the address fields are in
*
* change this to match the name of the group that contains your address fields
*/
$group = 'address';
// this is in case there a multiple instances of this template on a page
if ( !function_exists( 'xnau_gmap_src' ) ) :
/**
* generates the SRC value for a Google Maps iframe embed from the address fields of a PDB record
*
* @param object $fields a PDB record fields object ($this->record->$group->fields)
* @return string the src value for the iframe
*/
function xnau_gmap_src( $fields )
{
/**
* these are the fieldnames for the address
*
* change these to match the names of the address fields in your setup
*/
$address_fields = array('address', 'city', 'state', 'country');
// assemble the address search string from the address fields
$address = array();
foreach ( $address_fields as $field ) {
$v = $fields->$field->value;
if ( !empty( $v ) )
$address[] = $v;
}
// convert the array of values into a comma-separated list
$search = implode( ',', $address );
/*
* this is where the default parameters of the map are set with notes on some
* of the more useful ones. There are more, these are just the ones most likely
* to need setting. A full explanation of the parameters can be found here:
* https://moz.com/ugc/everything-you-never-wanted-to-know-about-google-maps-parameters
*/
$gmap_params = array(
'q' => $search, // address search
'mrt' => 'loc', // search type: loc = location, yp - business
't' => 'm', // map type: m - map, k - satellite, h - hybrid, p - terrain
'z' => 10, // zoom level: 1 - 20
'om' => 0, // overview map: 1 - show, 0 - don't show
'output' => 'embed', // output type
'f' => 'q', // search form type: q - standard, d - directions, l - local
);
return 'https://maps.google.com/maps?' . build_query( $gmap_params );
}
endif;
?>
<style>
.googlemap-embed {
width:99%;
height:350px;
border: 1px solid #777777;
}
</style>
<div class="wrap <?php echo $this->wrap_class ?>">
<?php while ( $this->have_groups() ) : $this->the_group(); ?>
<section class="<?php $this->group->print_class() ?> field-group" style="overflow:auto">
<?php $this->group->print_title( '<h2 class="field-group-title" >', '</h2>' ) ?>
<?php $this->group->print_description( '<p>', '</p>' ) ?>
<?php
while ( $this->have_fields() ) : $this->the_field();
// CSS class for empty fields
$empty_class = $this->get_empty_class( $this->field );
?>
<dl class="dl-horizontal <?php echo Participants_Db::$prefix . $this->field->name ?>">
<dt class="<?php echo $this->field->name . ' ' . $empty_class ?>"><?php $this->field->print_label() ?></dt>
<dd class="<?php echo $this->field->name . ' ' . $empty_class ?>"><?php $this->field->print_value() ?></dd>
</dl>
<?php endwhile; // end of the fields loop ?>
</section>
<?php endwhile; // end of the groups loop ?>
<dl class="locationmap">
<dt>Map</dt>
<dd>
<iframe class="googlemap-embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="<?php echo xnau_gmap_src( $this->record->{$group}->fields ) ?>"></iframe>
</dd>
</dl>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment