-1

I want to pass variable to google map script from php or html. but this is not working.

<?php
$x1=58.983991;
$y1=5.734863;
?>

<script>
var myCenter=new google.maps.LatLng($x1,$y1);
</script>
  • possible duplicate of [Pass a PHP string to a Javascript variable (and escape newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – Mark Amery Dec 21 '13 at 10:26

3 Answers3

0

Do it like this:

var myCenter=new google.maps.LatLng(<?php echo $x1; ?>,<?php echo $y1; ?>);

You can open PHP anywhere on the webpage until and unless the extension of the page is .php.

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
0
<?php
$x1=58.983991;
$y1=5.734863;

?>


<script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>

<script>

var myCenter=new google.maps.LatLng(<?php echo $x1?>,<?php echo $y1?>);

</script>
Vibin TV
  • 822
  • 9
  • 28
0

Use echo Short tag of php to print values .

 <?php
    $x1=58.983991;
    $y1=5.734863;
    ?>        
    <script>

    var myCenter=new google.maps.LatLng(<?=$x1; ?>,<?=$y1;?>);
    </script>
Vivek S
  • 2,010
  • 1
  • 13
  • 15