Passing php variables to th google.maps javascript method

I am trying to pass two variables (latitude and longitude) to the google.maps.StreetViewPanorama method without success. Is it possible to do that or I am failing ?

The variables are passing via the url:

http://mydomain/street.php?lat=41.653048&lon=-0.880677

The code of street.php is:

<?php
$lat = $_GET["lat"];
$lon = $_GET["lon"];
?>

<html>
    <head>

        <meta name='viewport' content='width=320,user-scalable=no'/>

        <script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript' >
          jlat = <?php echo $lat; ?>
          jlon = <?php echo $lon; ?>
        </script>

    </head>

    <body onload="new google.maps.StreetViewPanorama(document.getElementById('p'),
        {position:new google.maps.LatLng(jlat, jlon)
        });"

     style='padding:0px;margin:0px;'>

     <div id='p' style='height:100%;width:100%;'></div>

    </body>
</html>

I the i have fefined the new javescript variable

jlat = <?php echo $lat ?>
jlon = <?php echo $lon ?>

that hen i pass to the google.maps.LatLng(jlat, jlon), but it doesn't work.

Any idea about that?

Thanks in advance

Answers


I don't think the way you're writing your javascript is going to work. Specifically, if you're including javascript from external file, you shouldn't include more code in it. Try changing your code to this:

<?php
$lat = $_GET["lat"];
$lon = $_GET["lon"];
?>

<html>
    <head>
        <meta name='viewport' content='width=320,user-scalable=no'/>

        <script type="text/javascript">
            var jlat = <?php echo $lat ?>;
            var jlon = <?php echo $lon ?>;
        </script>

        <script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript' >
        </script>
    </head>

    <body onload="new google.maps.StreetViewPanorama(document.getElementById('p'),
        {position:new google.maps.LatLng(jlat, jlon)});" style='padding:0px;margin:0px;'>

     <div id='p' style='height:100%;width:100%;'></div>

    </body>
</html>

Didn't you forget a semicolon after $lat = $_GET["lat"]


Now add the semicolons in JavaScript...

var jlat = <?php echo $lat ?>;
var jlon = <?php echo $lon ?>;

Need Your Help

OutOfMemoryException - out of ideas

c# .net profiling out-of-memory

I know there is no simple answer to my question but I would appreciate ideas, guides or

jquery notation

php jquery ajax

I'm busy debugging someone else's code and I've come across something I think is odd.