我是乞gg,所以我有一个复杂的项目。我有一张每10秒钟刷新一次的地图,还有一个为每个ID生成坐标,时间和事件(标记)的按钮。当我第一次按下该按钮时,将为奇数id生成新数据,当我再次按下它时,将为偶数id生成新数据。
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);
}
更新脚本:
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++;
},
});
}
控制器:
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;
}
您可以在callAjax()中看到,有命令markerLayer.clearLayers();
删除带有标记的图层,并在循环结束时再次创建图层。这对我来说是个问题,因为它会刷新地图并再次写入新标记和旧标记。
我能以某种方式阻止重写不变的标记吗?
您正在将每个新图层添加到markerLayer
和地图中。从markerLayer
中清除图层不会从地图中清除它们。建议您不要将它们添加到地图,因此请更改
marker.addTo(markerLayer).addTo(map);
to
marker.addTo(markerLayer);
已将markerLayer
添加到地图一次。