Being a beginner, I am facing a complex project. The map refreshes every 10 seconds and there is a button that generates coordinates, time, and events (markers) for each ID. When I click the button for the first time, new data is generated for odd IDs, and when clicked again, it is generated for even IDs.
Ajax:
function callAjax() {
console.log('ajax');
$.ajax({
url: '/device_new',
type: 'GET',
dataType: 'json',
success: function(data) {
var coordinates = data;
markerLayer.clearLayers();
for (var i = 0; i < coordinates.length; i++) {
var icon = getMarkerType(coordinates[i].event);
if (coordinates[i].x && coordinates[i].y) {
marker = L.marker([coordinates[i].x, coordinates[i].y], {
icon: icon
})
.bindPopup("Device ID: " + coordinates[i].deviceId + '<br>' + "Time: " + coordinates[i].datetime);
marker.addTo(markerLayer).addTo(map);
}
}
map.fitBounds(markerLayer.getBounds(), { padding: [50, 50] });
},
});
setTimeout(callAjax, 10000);
}
Update script:
function updatedev() {
$.ajax({
//the route pointing to the post function
url: '/device/update/',
data: {
id: updateDeviceNew
},
type: 'GET',
// remind that 'data' is the response of the AjaxController
success: function(data) {
updateDeviceNew++;
},
});
}
Controller:
public function update(Request $request)
{
$value =$request->id;
$number=Device_new::get()->count();
//while i < broj zapisa u tabeli $device_new = Device_new::where('deviceId',1)->update(['x' => $x, 'y' => $y, 'datetime' => $datetime]); prvi put za neparne drugi put za poarne itd
if($value%2!=0)
$i=1;
else
$i=2;
for ($i; $i<=$broj; $i+=2){
$x = rand(44450000, 45000000) / 1000000;
$y = rand(16400000, 17900000) / 1000000;
$event=rand(1,4);
$datetime = new Carbon('now','Europe/Belgrade');
$device_new = Device_new::where('deviceId',$i)->update(['x' => $x, 'y' => $y, 'datetime' => $datetime, 'event' => $event]);
}
return $number;
}
In the callAjax() function, there is a command markerLayer.clearLayers();
that removes the layer with markers and recreates it at the end of the loop. This causes a problem for me as it refreshes the map and displays both old and new markers again.
Is there a way to prevent rewriting unchanged markers?