When a user signs up to my application (college project), they are asked for their address. I convert this address to latitude and longitude and then store it in my database.
I access the latitude and longitude like so:
<?php
require '../connect.php';
session_start();
$_SESSION['username'];
$sql = "SELECT latitude, longitude FROM userinformation WHERE username = '". $_SESSION['username']. "'";
$result = $db->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$lat = $row["latitude"];
$lng = $row["longitude"];
echo $lat;
echo $lng;
}
}
?>
I then try to pass the $lat and $lng variable into the Maps API script like so:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: {lat: '<?php echo $lat;?>', lng: '<?php echo $lng;?>'}
});
marker = new google.maps.Marker({
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
position: {lat: '<?php echo $lat;?>', lng: '<?php echo $lng;?>'}
});
marker.addListener('click', toggleBounce);
}
This gives me the following page:
I have also tried converted the PHP variables to JS:
var js_variable = '<?php echo $lat;?>';
document.write(js_variable);
var js_variable1 = '<?php echo $lng;?>';
document.write(js_variable1);
I then pass in the JS variables instead of the PHP variables, however the output is still the same. What would be the correct way to pass a variable as lat/lng instead of a hardcoded value:
center: {lat: 59.325, lng: 18.070}
position: {lat: 59.327, lng: 18.067}