add waypoint markers layer to map

This commit is contained in:
liamcottle
2024-03-18 22:52:28 +13:00
parent 375504dba0
commit f8fc4ad3e1

View File

@ -42,6 +42,11 @@
border: 1px solid white; border: 1px solid white;
} }
.waypoint-label {
font-size: 26px;
background-color: transparent;
}
.link { .link {
color: #2563eb; color: #2563eb;
} }
@ -973,6 +978,7 @@
var nodes = []; var nodes = [];
var nodeMarkers = {}; var nodeMarkers = {};
var selectedNodeOutlineCircle = null; var selectedNodeOutlineCircle = null;
var waypoints = [];
// set map bounds to be a little more than full size to prevent panning off screen // set map bounds to be a little more than full size to prevent panning off screen
var bounds = [ var bounds = [
@ -1002,6 +1008,7 @@
showCoverageOnHover: false, showCoverageOnHover: false,
disableClusteringAtZoom: 10, // zoom level where goToNode zooms to disableClusteringAtZoom: 10, // zoom level where goToNode zooms to
}); });
var waypointsLayerGroup = new L.LayerGroup();
// create icons // create icons
var iconOnline = L.divIcon({ var iconOnline = L.divIcon({
@ -1023,10 +1030,12 @@
var baseLayers = { var baseLayers = {
"Nodes (All)": nodesLayerGroup, "Nodes (All)": nodesLayerGroup,
"Nodes (Clustered)": nodesClusteredLayerGroup, "Nodes (Clustered)": nodesClusteredLayerGroup,
"Nodes (Hide All)": new L.LayerGroup(),
}; };
var overlays = { var overlays = {
"Neighbours": neighboursLayerGroup, "Neighbours": neighboursLayerGroup,
"Waypoints": waypointsLayerGroup,
}; };
// add layers to control ui // add layers to control ui
@ -1192,6 +1201,10 @@
neighboursLayerGroup.clearLayers(); neighboursLayerGroup.clearLayers();
} }
function clearAllWaypoints() {
waypointsLayerGroup.clearLayers();
}
function closeAllPopups() { function closeAllPopups() {
map.eachLayer(function(layer) { map.eachLayer(function(layer) {
if(layer.options.pane === "popupPane"){ if(layer.options.pane === "popupPane"){
@ -1238,14 +1251,12 @@
closeAllTooltips(); closeAllTooltips();
clearAllNodes(); clearAllNodes();
clearAllNeighbours(); clearAllNeighbours();
clearAllWaypoints();
clearNodeOutline(); clearNodeOutline();
} }
function onNodesUpdated(updatedNodes) { function onNodesUpdated(updatedNodes) {
// clear previous data
clearMap();
// clear nodes cache // clear nodes cache
nodes = []; nodes = [];
@ -1388,6 +1399,75 @@
} }
function onWaypointsUpdated(updatedWaypoints) {
// clear nodes cache
waypoints = [];
// add nodes
for(var waypoint of updatedWaypoints){
// skip expired waypoints
if(waypoint.expire < Date.now() / 1000){
continue;
}
// skip nodes without position
if(!waypoint.latitude || !waypoint.longitude){
continue;
}
// fix lat long
waypoint.latitude = waypoint.latitude / 10000000;
waypoint.longitude = waypoint.longitude / 10000000;
var hasLocation = isValidLatLng(waypoint.latitude, waypoint.longitude);
if(hasLocation){
// wrap longitude for shortest path, everything to left of australia should be shown on the right
var longitude = parseFloat(waypoint.longitude);
if(longitude <= 100){
waypoint.longitude = (longitude += 360);
}
// determine emoji to show as marker icon
const emoji = waypoint.icon === 0 ? 128205 : waypoint.icon;
const emojiText = String.fromCodePoint(emoji)
var tooltip = getTooltipContentForWaypoint(waypoint);
// create waypoint marker
var marker = L.marker([waypoint.latitude, waypoint.longitude], {
icon: L.divIcon({
className: 'waypoint-label',
iconSize: [26, 26], // increase from 12px to 26px
html: emojiText,
}),
}).bindPopup(tooltip).on('click', function(event) {
// close tooltip on click to prevent tooltip and popup showing at same time
event.target.closeTooltip();
});
// show tooltip on desktop only
if(!isMobile()){
marker.bindTooltip(tooltip, {
interactive: true,
});
}
// add marker to waypoints layer groups
marker.addTo(waypointsLayerGroup);
// add to cache
waypoints.push(waypoint);
}
}
}
function setLoading(loading){ function setLoading(loading){
var reloadButton = document.getElementById("reload-button"); var reloadButton = document.getElementById("reload-button");
if(loading){ if(loading){
@ -1402,6 +1482,9 @@
// show loading // show loading
setLoading(true); setLoading(true);
// clear previous data
clearMap();
// fetch nodes // fetch nodes
fetch('/api/v1/nodes').then(async (response) => { fetch('/api/v1/nodes').then(async (response) => {
@ -1419,6 +1502,15 @@
}); });
// fetch waypoints
fetch('/api/v1/waypoints').then(async (response) => {
// update waypoints
var json = await response.json();
onWaypointsUpdated(json.waypoints);
});
} }
function getTooltipContentForNode(node) { function getTooltipContentForNode(node) {
@ -1458,6 +1550,21 @@
} }
function getTooltipContentForWaypoint(waypoint) {
var tooltip = `<b>${waypoint.name}</b>` +
`<br/>${waypoint.description}` +
`<br/><br/>Expires: ${moment(new Date(waypoint.expire * 1000)).fromNow()}` +
`<br/>Lat/Lng: ${waypoint.latitude}, ${waypoint.longitude}`;
// bottom info
tooltip += `<br/><br/>ID: ${waypoint.waypoint_id}`;
tooltip += `<br/>Updated: ${moment(new Date(waypoint.updated_at)).fromNow()}`;
return tooltip;
}
// parse url params // parse url params
var queryParams = new URLSearchParams(location.search); var queryParams = new URLSearchParams(location.search);
var queryNodeId = queryParams.get('node_id'); var queryNodeId = queryParams.get('node_id');