Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active September 5, 2022 13:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save neilgee/a8cf60790c275508c03dd06bc5b48683 to your computer and use it in GitHub Desktop.
Save neilgee/a8cf60790c275508c03dd06bc5b48683 to your computer and use it in GitHub Desktop.
ACF Google Map - Get Directions Link
(function($) {
/*
* new_map
*
* This function will render a Google Map onto the selected jQuery element
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $el (jQuery element)
* @return n/a
*/
function new_map( $el ) {
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : 16,
center : new google.maps.LatLng(0, 0),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// create map
var map = new google.maps.Map( $el[0], args);
// add a markers reference
map.markers = [];
// add markers
$markers.each(function(){
add_marker( $(this), map );
});
// center map
center_map( map );
// return
return map;
}
/*
* add_marker
*
* This function will add a marker to the selected Google Map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $marker (jQuery element)
* @param map (Google Map object)
* @return n/a
*/
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
infowindow.open( map, marker );
});
}
}
/*
* center_map
*
* This function will center the map, showing all markers attached to this map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param map (Google Map object)
* @return n/a
*/
function center_map( map ) {
// vars
var bounds = new google.maps.LatLngBounds();
// loop through all markers and create bounds
$.each( map.markers, function( i, marker ){
var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
bounds.extend( latlng );
});
// only 1 marker?
if( map.markers.length == 1 )
{
// set center of map
map.setCenter( bounds.getCenter() );
map.setZoom( 16 );
}
else
{
// fit to bounds
map.setCenter( bounds.getCenter() );
map.setZoom( 2 ); // Change the zoom value as required
//map.fitBounds( bounds ); // This is the default setting which I have uncommented to stop the World Map being repeated
}
}
/*
* document ready
*
* This function will render each map when the document is ready (page has loaded)
*
* @type function
* @date 8/11/2013
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
// global var
var map = null;
jQuery(document).ready(function($){
$('.acf-map').each(function(){
// create map
map = new_map( $(this) );
});
});
})(jQuery);
<?php //<~ don't add me in
// Add in your functions.php
add_action( 'wp_enqueue_scripts', 'themeprefix_google_map_script' ); // Firing the JS and API
// Enqueue Google Map scripts
function themeprefix_google_map_script() {
wp_enqueue_script( 'google-map', get_stylesheet_directory_uri() . '/js/dealer.js', array( 'jquery' ), '1.0.0', true );
wp_enqueue_script( 'google-api', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDp1yayYfK-qOod7Kpt2x8u3Q8ZiuxMybc', null, null, true); // Add in your key
}
<?php //<~ don't add me in
add_action( 'genesis_entry_content', 'themeprefix_add_marker_loop' );
// Output all dealers via a custom loop of the Dealer CPT and show the title and address field in the marker and link that to the Dealer CPT
function themeprefix_add_marker_loop() {
$args = array(
'post_type' => 'dealer',
'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
echo "<div class='map-container'><div class='wrap'><div class='acf-map'>";
while ( $the_query->have_posts() ) : $the_query->the_post();
$location = get_field('location');
$title = get_the_title(); // Get the title
if( !empty($location) ) {
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></h4>
<p class="address"><?php echo $location['address']; ?></p>
</div>
<?php
}
endwhile;
echo '</div></div></div>';
wp_reset_postdata();
}
// Source - http://stackoverflow.com/questions/15042283/current-location-google-maps-link-to-directions
// Add markup in between .marker div
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<a href="https://www.google.com/maps?saddr=My+Location&daddr=<?php echo $location['address']; ?>" target="_blank"><?php _e('Get Directions','yourtheme'); ?></a>
</div>
<?php //<~ don't add me in
add_shortcode( 'themeprefix_multiple_marker', 'themeprefix_multiple_marker' ); // Create shortcode [themeprefix_multiple_marker]
// Output all dealers via a custom loop of the Dealer CPT and show the title and address field in the marker and link that to the Dealer CPT
function themeprefix_multiple_marker() {
ob_start();
$args = array(
'post_type' => 'dealer',
'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
echo "<div class='map-container'><div class='wrap'><div class='acf-map'>";
while ( $the_query->have_posts() ) : $the_query->the_post();
$location = get_field('location');
$title = get_the_title(); // Get the title
if( !empty($location) ) {
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></h4>
<p class="address"><?php echo $location['address']; ?></p>
</div>
<?php
}
endwhile;
echo '</div></div></div>';
wp_reset_postdata();
return ob_get_clean();
}
<?php //<~ don't add me in
add_action( 'genesis_entry_content', 'themeprefix_add_marker' ); // Hook in the field
// ACF Google Map Single Map Output
function themeprefix_add_marker() {
$location = get_field('location'); // Set the ACF location field to a variable
if( !empty($location) ) {
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></h4> <!-- Output the title -->
<p class="address"><?php echo $location['address']; ?></p> <!-- Output the address -->
</div>
</div>
<?php
}
}
<?php //<~ don't add me in
add_shortcode( 'themeprefix_single_marker', 'themeprefix_single_marker' ); // Create shortcode [themeprefix_single_marker]
// ACF Google Map Single Map Output
function themeprefix_single_marker() {
ob_start();
$location = get_field('location'); // Set the ACF location field to a variable
if( !empty($location) ) {
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><a href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></h4> <!-- Output the title -->
<p class="address"><?php echo $location['address']; ?></p> <!-- Output the address -->
</div>
</div>
<?php
}
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment