window.onload = function()
{
window.addEventListener('resize',function()
{
map.width = window.innerWidth;
map.canvas.height = window.innerHeight;
},false);
};
var stationId = 1;
var mapWith = 6594;
var mapLength = 10000;
var mapMaxBounds = L.latLng(mapWith, mapLength);
var mapCenter = L.latLng(mapWith/2, mapLength/2);
var map = L.map('map',
{
crs: L.CRS.Simple,
minZoom: -5,
maxZoom: 3,
zoomDelta: 0.25,
maxBounds: [[0,0], mapMaxBounds],
center: mapCenter,
zoomControl: true,
attributionControl: false
}
); //renderer: L.canvas(),
var lineGroup = L.featureGroup();
var lineGroupMas = [];
var carGroupCircle = L.featureGroup();
var carGroup = L.featureGroup();
var stationPolygonGroup = L.featureGroup();
var bounds = [[0,0], [mapWith, mapLength]];//границы
var image = L.imageOverlay('map/electrostal10000x6594.jpg', bounds).addTo(map); //подгрузка имейджа
map.fitBounds(bounds);
const promiseStat = getStationPolygon();
promiseStat.then((value) =>
{
stationPolygonGroup.addTo(map);//добавляем слой полигонов на карту
if(stationId == 1)
{setInterval(() => getTrInfo(lineGroupMas), 15000);};
});
});
async function getTrainInfo(polylinesMass){
let xhr = new XMLHttpRequest();
xhr.open("GET", 'http://localhost:8080/DispProj/getTrInfoServlet', true);
xhr.responseType = "json";
xhr.send();
xhr.onload = () =>
{
if (xhr.status === 200)
{
let paths = xhr.response;
for(var i=0; i < paths.length; i++)
{
carGroup.clearLayers();
if(paths[i].locoTr.length == 0)
{
drawLocoClosestWay(
paths[i].locoPixelX,
paths[i].locoPixelY,
8,
16,
paths[i].locoNum,
paths[i].locoID,
paths[i].locoColor,
paths[i].locoFillColor,
paths[i].locoFillOpacity,
polylinesMass
);
}
else
drawLocoClosestWay(
paths[i].locoPixelX,
paths[i].locoPixelY,
8,
16,
paths[i].locoNum,
paths[i].locoID,
paths[i].locoColor,
paths[i].locoFillColor,
paths[i].locoFillOpacity,
polylinesMass
);
};
}
else alert(`${xhr.status}: ${xhr.statusText}`);
};
xhr.onerror = () => {alert(`Ошибка сети. Сервер не отдал код ошибки.`);};
}
function drawLocoClosestWay(x, y, carWidth, carLength, carNum, carID, colorObj, colorFillObj, colorFillOpacity, polylineMass)
{
let closestDotPathMas = getClosestWay(x, y, polylineMass);
let closestLatLng = closestDotPathMas[0];
makeCarPolygon(closestLatLng.lat, closestLatLng.lng, carWidth, carLength, carNum, carID, colorObj, colorFillObj, colorFillOpacity);
return closestDotPathMas[3];
}
function makeCarPolygon(x, y, carWidth, carLangth, carNum, carID, colorObj, colorFillObj, colorFillOpacity)
{
let halfWidth = carWidth/2;
let halfLangth = carLangth/2;
var polygon = L.polygon([[x+halfWidth, y+halfLangth],
[x+halfWidth, y-halfLangth],
[x-halfWidth, y-halfLangth],
[x-halfWidth, y+halfLangth]],
{color: colorObj, fillColor: '#006400', autoPan: false, fillOpacity: 0.5}
);//.addTo(map)
map.fitBounds(polygon.getBounds());
polygon.bindPopup(carNum+'', {autoPan: false});
carGroup.addLayer(polygon);
carGroup.addTo(map);
carGroup.bringToFront();
/*polygon5.bindPopup('<p><a href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" target="_blank"> Hello world!<br />This is a nice popup.</a></p>');*/
};