Search

Pin Free Nodes

MapWeave Basics

Pin nodes in position after a user has dragged them.

Pin Free Nodes
View live example →

Drag a node to a new location to pin it. Zoom in and out on the map to see what happens.

By default, free nodes can adjust their positions to achieve an optimal graph layout. You can use the pin() APIpin prop to prevent this for specific nodes.

import { MapWeave } from "mapweave/mapbox";
import { NetworkLayer } from "mapweave/layers";
import { networkData, defaultView } from "./data";

const pinnableIds = ["A", "B", "C", "D", "E"];
const infoboxContent = document.getElementById("infobox-content");

function formatList(items) {
  return items.length === 0 ? "None" : items.join(", ");
}

function listPinnedNodes() {
  const pinned = networkLayer.pin();
  const pinnedIds = pinnableIds.filter((id) => pinned[id] !== undefined);
  const unpinnedIds = pinnableIds.filter((id) => pinned[id] === undefined);
  infoboxContent.textContent = `Unpinned: ${formatList(unpinnedIds)}\nPinned: ${formatList(pinnedIds)}`;
}

const mapweave = new MapWeave({ container: "mw", options: { accessToken: VITE_MAPBOX_API_KEY } });
const networkLayer = new NetworkLayer({ data: networkData });
mapweave.addLayer(networkLayer);
mapweave.view(defaultView);

mapweave.on("drag-end", (e) => {
  if (e.position === null || e.id === null) {
    return;
  }

  const currentPinned = networkLayer.pin();
  networkLayer.pin({ ...currentPinned, [e.id]: e.position });

  listPinnedNodes();
});

document.getElementById("unpin").addEventListener("click", () => {
  networkLayer.pin({});
  listPinnedNodes();
});

listPinnedNodes();
const latitude = 42.6334;
const longitude = -71.3162;

const nodeSize = 10;
const labelFontSize = 12;

export const networkData = {
  node1: {
    type: "node",
    latitude,
    longitude,
    color: "#2471a3",
    size: nodeSize,
  },
};

for (const id of "ABCDE") {
  networkData[id] = {
    type: "node",
    color: "#27ae60",
    size: nodeSize,
    label: {
      text: id,
      position: "center",
      fontSize: labelFontSize,
      backgroundColor: "rgba(0,0,0,0)",
    },
  };

  networkData[`link${id}`] = {
    type: "link",
    color: "#34495e",
    id1: id,
    id2: "node1",
  };
}

export const defaultView = {
  latitude: 42.6334,
  longitude: -71.3162,
  zoom: 6,
};
<!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">
        <pre id="infobox-content"></pre>
        <button id="unpin">Unpin All</button>
      </div>
    </div>
    <script type="module" src="./code.js"></script>
  </body>
</html>
import React, { useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { MapWeave } from "mapweave/react/mapbox";
import { NetworkLayer } from "mapweave/react/layers";

import { networkData, defaultView } from "./data";

const mapWeaveOptions = { accessToken: VITE_MAPBOX_API_KEY };

const pinnableIds = ["A", "B", "C", "D", "E"];
const infoboxStyle = { position: "absolute", margin: 5 };

function formatList(items) {
  return items.length === 0 ? "None" : items.join(", ");
}

function listPinnedNodes(pinned) {
  const pinnedIds = pinnableIds.filter((id) => pinned[id] !== undefined);
  const unpinnedIds = pinnableIds.filter((id) => pinned[id] === undefined);
  return `Unpinned: ${formatList(unpinnedIds)}\nPinned: ${formatList(pinnedIds)}`;
}

function Demo() {
  const networkLayer = useRef(null);
  const [pinnedNodes, setPinnedNodes] = useState({});

  return (
    <>
      <div className="example__infobox" style={infoboxStyle}>
        <pre>{}</pre>
        <button
          onClick={() => {
            setPinnedNodes({});
          }}
        >
          Unpin All
        </button>
      </div>
      <MapWeave
        options={mapWeaveOptions}
        view={defaultView}
        onDragEnd={({ id, position }) => {
          if (id === null || position === null) {
            return;
          }

          setPinnedNodes({ ...pinnedNodes, [id]: position });
        }}
      >
        <NetworkLayer data={networkData} pin={pinnedNodes} ref={networkLayer} />
      </MapWeave>
    </>
  );
}

const root = createRoot(document.getElementById("mw"));
root.render(<Demo />);
const latitude = 42.6334;
const longitude = -71.3162;

const nodeSize = 10;
const labelFontSize = 12;

export const networkData = {
  node1: {
    type: "node",
    latitude,
    longitude,
    color: "#2471a3",
    size: nodeSize,
  },
};

for (const id of "ABCDE") {
  networkData[id] = {
    type: "node",
    color: "#27ae60",
    size: nodeSize,
    label: {
      text: id,
      position: "center",
      fontSize: labelFontSize,
      backgroundColor: "rgba(0,0,0,0)",
    },
  };

  networkData[`link${id}`] = {
    type: "link",
    color: "#34495e",
    id1: id,
    id2: "node1",
  };
}

export const defaultView = {
  latitude: 42.6334,
  longitude: -71.3162,
  zoom: 6,
};
<!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>

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.