personally, I build arrays of coordinates in the blade view direct from the database (rather than fetching the data with ajax)
Laravel and Leaflet Mapping
I've been looking for quite a while on how to be able to use Laravel and Leaflet together to plot different assets and zones on a map. I want to store the lat, long, and other descriptions of the assets in a database since the information will be updated and changed over time.
What's the best way to get the data and loop through each asset and plot them by coordinates? I've been able to use geojson files to loop through the features but I'd much rather use a database?
I also have a more complex scenario where tooltips are created (along the same line actually)
Controller
$markers = $qualityEvents->map(function ($event, $key) {
return [
'latlng' => [$event->Latitude, $event->Longitude],
'time' => Carbon::parse($event->EventDateTime)->format('H:i'),
'date' => Carbon::parse($event->EventDateTime)->format('d-m-Y'),
'driver' => trim($event->DriverID ?? '--'),
'location' => $event->Location ?? '--',
'site' => $event->SiteID,
'speed' => intval(($event->CANSpeedInKilometresPerHour ?? $event->GPSSpeedInKilometresPerHour) * 0.621371192) . 'mph',
'course' => $event->GPSCourseInDegrees,
];
})->values();
pass this into the javascript part of the view as before
var markers = {!! json_encode($markers) !!}
and project to the map
//markers along the tracking line
markers.map( function(item) {
tip = '<h4>Time: ' + item.time + '<small> on '+ item.date +'</small><br />'
+ '<small>Location: ' + item.location + '</small><br />'
+ '<small>Driver: ' + item.driver + '</small><br />'
+ '<small>Speed: ' + item.speed; + '</small></h4>'
if(item.site == ''){
marker = L.marker(item.latlng,{rotationAngle: item.course, opacity:0})
.bindPopup(tip);
allmarkers.addLayer(marker);
} else {
L.circleMarker(item.latlng,{radius:7, color:'#0ff',stroke:false, fill:true, fillOpacity:1})
.addTo(mymap)
.bindPopup(tip);
}
})

Please or to participate in this conversation.