Skip to content

Instantly share code, notes, and snippets.

@mjn666
Last active December 30, 2015 05:29
Show Gist options
  • Select an option

  • Save mjn666/7782614 to your computer and use it in GitHub Desktop.

Select an option

Save mjn666/7782614 to your computer and use it in GitHub Desktop.
WordPress + ACF Location FieldFunction wrapper for outputting a GMAP using ACF's Location Field (http://www.advancedcustomfields.com/add-ons/google-maps/)To be used in the "loop".
/**
* Add Google Map via ACF Location field
*
* API added via @see theme/enqueue.php: https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false
*
* @param (int) $post_id
* @param (string) $width Width of the gmap to display
* @param (string) $height height of the gmap to display
*
* @return HTML for google map embed
*/
if ( ! function_exists('my_theme_gmap_location') ) {
function my_theme_gmap_location( $post_id = '', $width = '480px', $height = '320px' ) {
global $post;
$post_id = ( $post_id == '' ? $post->ID : $post_id);
if ( function_exists('get_field') ) :
if ( get_field( 'gmap_location', $post_id ) ) :
$location = get_field( 'gmap_location', $post_id );
$html = ob_start();
?>
<script>
var map;
var myLatLang = new google.maps.LatLng(<?php echo $location['coordinates']; ?>);
function initialize() {
var mapOptions = {
zoom: 13,
center: myLatLang,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map( document.getElementById('map-canvas'), mapOptions );
var marker = new google.maps.Marker({
position: myLatLang,
map: map,
});
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas" style="width:<?php echo $width; ?>; height:<?php echo $height; ?>"></div>
<?php
$html = ob_get_clean();
echo $html;
endif;
endif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment