I have a PHP array containing names and coordinates for infrastructure stored in a database. Here is how I am trying to pass it:
$data = array();
@endphp
@foreach ($infrastructures as $infrastructure)
@if ($loop->iteration)
@php
$name = $infrastructure->inf_name;
$coord = $infrastructure->inf_lat.",".$infrastructure->inf_long;
$data = $name.",". $coord;
@endphp
@endif
@endforeach
The result of the loop is (Red Beach Seawall,1.3582,172.9266 Buota Bridge,1.3901,173.1343 Nippon Causeway,1.3399,172.9579 Maaman Kaburara,0.403,173.9217 TUC Main Road,0.2264,182)
However, I want the output to be formatted like this in JavaScript.
<script>
var infras= <?= json_encode($infras); ?>;
var mymap = L.map('mapid').setView([1.3582,172.9266], 13);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 10,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: enter your accesstoken here
}).addTo(mymap);
for (var i = 0; i < infras.length; i++){
var marker = L.marker([infras[i][1],infras[i][2]]).addTo(mymap).bindPopup(infras[i][0]);
}
</script>
Could someone please help me with this?
Thank you