Meta values in wordpress linked to Google Maps lat,lng API

I have a custom function that adds one latitude- and one longitude meta box in WordPress.

I’m using the Google Maps API like this in my functions.php

Read More
function googlemaps() {
    echo "<script>

    var map;
    function initMap() {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
      });
    }

        </script>
     ";
    }

which works fine with static lat and lng values but it I can’t get it to work with my meta values

I did this, which is obviously wrong, but that’s as far as my knowledge takes me

function googlemaps() {
    echo "<script>

    var map;
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: get_post_meta( get_the_ID(), '_lat', lng: get_post_meta( get_the_ID(), '_boyta'},
        zoom: 8
      });
    }

        </script>
    ";
}

Related posts

1 comment

  1. You need to get out of your “echo” statement to print the result of get_post_meta. Right now, you’re just printing the text. Try this:

    function googlemaps() {
      echo "<script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: " . get_post_meta( get_the_ID(), '_lat', true ) . ", lng: " . get_post_meta( get_the_ID(), '_boyta', true ) . "},
          zoom: 8
        });
      }
    
    </script>";
    }
    

    I’m assuming your lat and long are stored as “_lat” and “_boyta”? The “true” I added tells WordPress that you want the single value, not an array of values.

Comments are closed.