I am reading data from MySQL using PHP. I am then trying to pass that data to the JS below within the same HTML, using it on OpenLayer maps. I have tried various methods including json_encode but cannot seem to pass valid data. When I set the data manually in the JS it works OK. What am I doing wrong with the code? Is there a preferred method?
<html lang="en">
.map { margin: 0; padding: 0; height: 100%; width: 100%; } OnTRack User Tacking
<?php
// ... Other database code removed for example...
$rowlon = 151.215324;
$rowlat = -33.856733;
?>
<div id="map" class="map"></div>
<script type="text/javascript">
// var latestlon = <?php echo $rowlon; ?>;
// var latestlat = <?php echo $rowlat; ?>;
var latestlon = "<?php echo json_encode($rowlon) ?>";
var latestlat = "<?php echo json_encode($rowlat) ?>";
// var latestlon = 151.215324;
// var latestlat = -33.856733;
var mycoords = [latestlon, latestlat];
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat(mycoords),
zoom: 12
})
});
</script>