-1

I have created a variable ($latlon) in php via an sql query like so...

// ...
if ($result->num_rows >0) {
    while ($row = $result->fetch_assoc()) {
        echo "id: " . $row["userid"] . latitude: . $row["latitude"] . 
        longitude: . $row["longitude"] ;

         $latlon = $row["latitude"] . "," . $row["longitude"];

and now i would like to use either the variable $latlon or $row["latitude"] & $row["longitude"] in some javascript code that displays a map which looks like this. Ideally i would like my php variable to go in here (51.508742,-0.120850) because these coords will continually change.

<script>
function initialize()
{
    var mapProp = {
        center: new google.maps.LatLng**(51.508742,-0.120850)**,
        zoom:7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}

function loadScript()
{
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/
    js?key=&sensor=false&callback=initialize";
    document.body.appendChild(script);
}
window.onload = loadScript;
</script>

any help will be appreciated. thank you

2
  • Provided your page has a php file extension you can use: center: new google.maps.LatLng(<?php echo $row["latitude"];?>,<?php echo $row["longitude"];?>), Commented Aug 28, 2015 at 17:59
  • thx everyone, gonna try all these suggestions back in a mo. Commented Aug 28, 2015 at 18:09

3 Answers 3

3

You should echo the $latlon variable inside php tag. So the line will be as following: new google.maps.LatLng(<?php echo $latlon;?>). I assume you have the correct syntax for the js in the php $latlon variable.

Remember, this will only work if it is a php file you are placing the code on.

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest you to do something like this:

<input type="hidden" id="lat" value="<?php echo $lat; ?>">
<input type="hidden" id="long" value="<?php echo $long; ?>">

And fetch those values using JS selector Like this:

var lat=document.getElementById('lat');
var long=document.getElementById('long');

3 Comments

i split my variable in two like this $lat = $row["latitude"]; & $lon = $row["longitude"];
then------ <input type="hidden" id="lat" value="<?php echo $lat; ?>"> <input type="hidden" id="lon" value="<?php echo $lon; ?>">
but only the frame of the map is showing, no detail.
0

Assuming that you are rendering the page using html you could could put that variable in a hidden input element and retrieve it from javascript later.

Like this:

<input id="latlon" type="hidden" name="name" "<?php echo htmlspecialchars($latlon); ?>" />

2 Comments

Please edit your answer with more of the text and an example before it gets flagged being low quality
hi toskv, how do i do that?

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.