Hide labels by default to reduce clutter in your map visualizations.
Here, the labels are revealed using the NetworkNodeLabel visible API with an onHover interaction.
- Event Handling docs
import { MapWeave } from "mapweave/mapbox";
import { NetworkLayer } from "mapweave/layers";
import { networkData } from "./data";
const mapweave = new MapWeave({ container: "mw", options: { accessToken: VITE_MAPBOX_API_KEY } });
const networkLayer = new NetworkLayer({ data: networkData });
mapweave.addLayer(networkLayer);
mapweave.fitBounds();
let lastHovered = null;
let hoverStyleReset = null;
mapweave.on("hover", ({ id }) => {
if (id !== null) {
if (id !== lastHovered) {
lastHovered = id;
hoverStyleReset?.();
hoverStyleReset = networkLayer.overrideStyle(id, {
scale: 1.4,
label: { visible: true },
border: { color: "white" },
});
}
} else {
lastHovered = null;
hoverStyleReset?.();
}
}); const cities = [
["London", 51.5072, -0.1275],
["Birmingham", 52.48, -1.9025],
["Portsmouth", 50.8058, -1.0872],
["Southampton", 50.9025, -1.4042],
["Nottingham", 52.9561, -1.1512],
["Bristol", 51.4536, -2.5975],
["Manchester", 53.479, -2.2452],
["Liverpool", 53.4094, -2.9785],
["Leicester", 52.6344, -1.1319],
["Worthing", 50.8147, -0.3714],
["Coventry", 52.4081, -1.5106],
["Belfast", 54.5964, -5.93],
["Bradford", 53.8, -1.75],
["Derby", 52.9247, -1.478],
["Plymouth", 50.3714, -4.1422],
["Wolverhampton", 52.5833, -2.1333],
["Northampton", 52.2304, -0.8938],
["Norwich", 52.628, 1.2928],
["Luton", 51.8783, -0.4147],
["Aberdeen", 57.15, -2.11],
["Croydon", 51.3727, -0.1099],
["Bournemouth", 50.72, -1.88],
["Basildon", 51.58, 0.49],
["Maidstone", 51.272, 0.529],
["Ilford", 51.557, 0.0858],
["Warrington", 53.39, -2.59],
["Oxford", 51.75, -1.25],
["Harrow", 51.5836, -0.3464],
["Gloucester", 51.8667, -2.25],
["York", 53.96, -1.08],
["Blackpool", 53.8142, -3.0503],
["Stockport", 53.4083, -2.1494],
["Glasgow", 55.862, -4.258],
["Edinburgh", 55.953, -3.188],
["Cambridge", 52.205, 0.1192],
];
function createNetworkNodes(cities) {
const networkNodes = {};
for (const [name, lat, lon] of cities) {
networkNodes[name] = {
type: "node",
size: 5,
latitude: lat,
longitude: lon,
color: "#0FAB7C",
border: { width: 1, color: "#0FAB7C" },
label: {
text: name,
position: "ne",
backgroundColor: "rgba(31, 31, 31, 0.75)",
color: "white",
border: { width: 1, color: "white" },
visible: false,
},
};
}
return networkNodes;
}
export const networkData = createNetworkNodes(cities); <!doctype html>
<html>
<head>
<link rel="stylesheet" href="@ci/theme/mw/css/examples.css" />
</head>
<body>
<div id="mw">
<div class="example__infobox" style="position: absolute; margin: 5px">
Hover a node to reveal the label.
</div>
</div>
<script type="module" src="./code.js"></script>
</body>
</html> import React, { useState } from "react";
import { createRoot } from "react-dom/client";
import { MapWeave } from "mapweave/react/mapbox";
import { NetworkLayer } from "mapweave/react/layers";
import { networkData } from "./data";
const mapWeaveOptions = { accessToken: VITE_MAPBOX_API_KEY };
const overrideStyle = { scale: 1.4, label: { visible: true }, border: { color: "white" } };
function Demo() {
const [hoverInfo, setHoverInfo] = useState(null);
const overrideStyles =
hoverInfo?.item?.type === "node" ? [{ ids: hoverInfo.id, overrideStyle }] : [];
return (
<>
<div className="example__infobox" style={{ position: "absolute", margin: 5 }}>
Hover a node to reveal the label.
</div>
<MapWeave options={mapWeaveOptions} onHover={setHoverInfo}>
<NetworkLayer data={networkData} overrideStyles={overrideStyles} />
</MapWeave>
</>
);
}
const root = createRoot(document.getElementById("mw"));
root.render(<Demo />); const cities = [
["London", 51.5072, -0.1275],
["Birmingham", 52.48, -1.9025],
["Portsmouth", 50.8058, -1.0872],
["Southampton", 50.9025, -1.4042],
["Nottingham", 52.9561, -1.1512],
["Bristol", 51.4536, -2.5975],
["Manchester", 53.479, -2.2452],
["Liverpool", 53.4094, -2.9785],
["Leicester", 52.6344, -1.1319],
["Worthing", 50.8147, -0.3714],
["Coventry", 52.4081, -1.5106],
["Belfast", 54.5964, -5.93],
["Bradford", 53.8, -1.75],
["Derby", 52.9247, -1.478],
["Plymouth", 50.3714, -4.1422],
["Wolverhampton", 52.5833, -2.1333],
["Northampton", 52.2304, -0.8938],
["Norwich", 52.628, 1.2928],
["Luton", 51.8783, -0.4147],
["Aberdeen", 57.15, -2.11],
["Croydon", 51.3727, -0.1099],
["Bournemouth", 50.72, -1.88],
["Basildon", 51.58, 0.49],
["Maidstone", 51.272, 0.529],
["Ilford", 51.557, 0.0858],
["Warrington", 53.39, -2.59],
["Oxford", 51.75, -1.25],
["Harrow", 51.5836, -0.3464],
["Gloucester", 51.8667, -2.25],
["York", 53.96, -1.08],
["Blackpool", 53.8142, -3.0503],
["Stockport", 53.4083, -2.1494],
["Glasgow", 55.862, -4.258],
["Edinburgh", 55.953, -3.188],
["Cambridge", 52.205, 0.1192],
];
function createNetworkNodes(cities) {
const networkNodes = {};
for (const [name, lat, lon] of cities) {
networkNodes[name] = {
type: "node",
size: 5,
latitude: lat,
longitude: lon,
color: "#0FAB7C",
border: { width: 1, color: "#0FAB7C" },
label: {
text: name,
position: "ne",
backgroundColor: "rgba(31, 31, 31, 0.75)",
color: "white",
border: { width: 1, color: "white" },
visible: false,
},
};
}
return networkNodes;
}
export const networkData = createNetworkNodes(cities); <!doctype html>
<html>
<head>
<link rel="stylesheet" href="@ci/theme/mw/css/examples.css" />
</head>
<body>
<div id="mw"></div>
<script type="module" src="./code.jsx"></script>
</body>
</html>