Reputation: 101
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
Upvotes: -1
Views: 183
Reputation: 31600
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); ?>" />
Upvotes: 0
Reputation: 184
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.
Upvotes: 3
Reputation: 36609
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');
Upvotes: 0