1

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

3
  • What do you mean by "doesn't work"? What exactly happens? As a side note, I would probably declare those two variables with var lat = ... and var lon = ... Commented Jun 11, 2012 at 13:27
  • The scripts works if I do not use the variables, for example:<body onload="new google.maps.StreetViewPanorama(document.getElementById('p'), {position:new google.maps.LatLng(41.653048, -0.880677) });" but doesn't with the variables jlat and jlon Commented Jun 11, 2012 at 13:34
  • You still didn't explain what you mean by "doesn't work" What happens? What errors are you getting? Commented Jun 11, 2012 at 13:35

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

+1 for spotting it, although I don't know if this is the root cause. The OP doesn't really say what he means by "doesn't work"
1

Now add the semicolons in JavaScript...

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

1 Comment

Thank you, the poroblem was as Aleks G said and semicolons in javascript!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.