Skip to content

Instantly share code, notes, and snippets.

@mattradford
Last active July 24, 2024 18:08
Show Gist options
  • Select an option

  • Save mattradford/bb7679a2671b99ada655 to your computer and use it in GitHub Desktop.

Select an option

Save mattradford/bb7679a2671b99ada655 to your computer and use it in GitHub Desktop.
ACF Get Directions map link
<?php
$location = get_field('map_location');
if ( !empty( $location ) ) :
$map_url = 'https://www.google.com/maps/dir/?api=1&destination=' . $location['lat'] . ',' . $location['lng'];
echo '<a href=". esc_url( $map_url ) . '" rel="nooopener">Get directions</a>';
endif;
?>
<p>This should produce a link like this:</p>
<a href="https://www.google.com/maps/dir/?api=1&destination=51.072159,1.088130">Get directions</a>
See <a href="https://developers.google.com/maps/documentation/urls/get-started#directions-action">https://developers.google.com/maps/documentation/urls/get-started#directions-action</a>
@samkent

samkent commented Sep 1, 2015

Copy link
Copy Markdown

Awesome, works perfectly! Thank you.

@ryan-noakes

ryan-noakes commented Jun 12, 2016

Copy link
Copy Markdown

Agreed, works perfectly. Thanks for posting this!

edit: I found that this does not work with mobile devices, however this works:
<a href="https://www.google.com/maps/place/<?php echo $location['address']; ?>" target="_blank"><?php _e('Get Directions','roots'); ?></a>
Desktop/laptop users have to click the Directions button when the Google Map page loads, but mobile users don't have the Maps app saying it can't use the address

@Frithir

Frithir commented Jun 15, 2016

Copy link
Copy Markdown

Sweet, thanks for that :)

@JetLewis

JetLewis commented Dec 13, 2017

Copy link
Copy Markdown

ryan-noakes answer won't work, you have to replace the spaces by + signs like a google map url would do :

https://www.google.com/maps/place/<?php echo str_replace(' ','+',$location['address']); ?>"

It works everytime for me and is smaller in code than the first answer.

@querysol

Copy link
Copy Markdown

Added urlencode:
$map_link = 'https://www.google.com/maps/place/' . urlencode( str_replace( ' ', '+' , $map['address'] ) );

@mitchbudreski

Copy link
Copy Markdown

this just saved me a lot of work thank you

ghost commented Feb 22, 2019

Copy link
Copy Markdown

Really thanks, but i change

$map_link = 'https://www.google.com/maps/place/' . urlencode( str_replace( ' ', '+' , $map['address'] ) );
by :
$map_link = 'https://www.google.com/maps/place/' . urlencode( str_replace( '% ', ' ' , $map['address'] ) );
it's little but its work better :)

@Applelo

Applelo commented Jul 4, 2019

Copy link
Copy Markdown

$map_link = 'https://www.google.com/maps/search/?api=1&query=' . urlencode( str_replace( '% ', ' ' , $map['address'] ) );
Check the https://developers.google.com/maps/documentation/urls/guide

@vnscorpion

vnscorpion commented Aug 3, 2019

Copy link
Copy Markdown

I use this:
$map_link = 'https://www.google.com/maps/dir/?api=1&destination=' . $location['lat'] .','. $location['lng'];
<a class="directions" href="'. $map_link .'" target="_blank">Chỉ đường</a>

@trungnl-git

Copy link
Copy Markdown

Is there anybody who got it worked? The one JetLewis provided don't work for me, it brings me just to Google Maps without any data from the ACF field

@Picpool

Picpool commented Mar 16, 2022

Copy link
Copy Markdown

it does not work for me...It send to the page with adress but the map is not centered and there is no marker

@mattradford

Copy link
Copy Markdown
Author

@Picpool The Maps API has changed a bit since 2015. I've updated the gist.

@itadakimasu9

Copy link
Copy Markdown

To create a link pointing directly to the location in Google Maps

<?php
$location = get_field('luogo_evento');
if (!empty($location)) {
   $street = $location['street_name'] . ' ' . $location['street_number'];
   $city = $location['city'];
   $address = urlencode("{$street}, {$city}");
   $lat = $location['lat'];
   $lng = $location['lng'];
   $map_link = "https://www.google.com/maps/search/?api=1&query=$address";
   echo '<a class="directions" href="' . $map_link . '" target="_blank">' . $street . ' - ' . $city . '</a>';
}
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment