Skip to content

Instantly share code, notes, and snippets.

@mattradford
Last active February 16, 2023 12:08
Show Gist options
  • Save mattradford/bb7679a2671b99ada655 to your computer and use it in GitHub Desktop.
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>
@itadakimasu9
Copy link

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