Support

Account

Home Forums General Issues Can we use get_field for a Script

Solved

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']; ?> },
    
  • Yes! Thank you so much!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.