Home › Forums › General Issues › Can we use get_field for a Script
Hello. Here I have a script to display google map with snazzy map styling copied from my old project which is a static location.
I want to replace the Lat & Lng dynamically with ACF map field. Not really familiar with the script. But would like to know if we could use the function get_field to get those values for these 2 lines?
center: new google.maps.LatLng(22.284687294722, 114.15541043858117),
position: { lat: 22.284687294722, lng: 114.15541043858117 },
With something like this?
$location = get_field(‘location’);
$lat = $location[‘lat’];
$lng $location[‘lng’];
Below is the part of the full code FYI. Thanks
<script type="text/javascript">
// When the window has finished loading create our google map below
google.maps.event.addDomListener(window, 'load', init);
function init() {
// Basic options for a simple Google Map
// For more options see: https://developers.google.com/maps/documentation/javascript/reference#MapOptions
var mapOptions = {
// How zoomed in you want the map to start at (always required)
zoom: 15,
center: new google.maps.LatLng(22.284687294722, 114.15541043858117),
disableDefaultUI: true,
// Let's also add a marker while we're at it
var image =
"https://example.com/Google-map-location.svg";
var beachMarker = new google.maps.Marker({
position: { lat: 22.284687294722, lng: 114.15541043858117 },
map,
icon: image,
});
It the code you included is in a PHP file, yes.
<?php
$location = get_field('field_name');
?>
... other code ...
position: { lat: <?php echo $location['lat']; ?>, lng: <?php echo $location['lng']; ?> },
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.