React Map Basic
A basic Woosmap Map sample using React JS Framework
Example
React Map Basic
import { createRoot } from "react-dom/client";
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from "react";
// The shim reads and writes `window.woosmap` before the API is loaded.
type LoaderWindow = {
woosmap?: {
map?: Record<string, (name: string, ...args: unknown[]) => unknown>;
};
};
const WOOSMAP_SDK_HOST = "https://sdk.woosmap.com";
// Installs the documented Woosmap loader shim (see docs/docs/loader.md), which
// defines a stub `woosmap.map.importLibrary` that fetches map.js on first call.
// Safe to call more than once: the shim itself no-ops (and warns) past the first.
function installLoaderShim(apiKey: string): void {
((g) => {
let p;
const w = window as unknown as LoaderWindow,
d = document;
const L = "importLibrary",
CB = "__ib__";
const E = "The Woosmap Maps JavaScript API";
const woosmap = w.woosmap || (w.woosmap = {});
const maps = woosmap.map || (woosmap.map = {});
const libs = new Set(),
params = new URLSearchParams();
const load = () =>
p ||
(p = new Promise((res, rej) => {
const s = d.createElement("script");
params.set("libraries", [...libs].join(","));
for (const k in g) params.set(k, g[k]);
params.set("callback", "woosmap.map." + CB);
s.src = `${WOOSMAP_SDK_HOST}/map/map.js?` + params;
maps[CB] = res;
s.onerror = () => rej(Error(E + " could not load."));
d.head.append(s);
}));
if (maps[L]) {
console.warn(E + " only loads once. Ignoring:", g);
} else {
maps[L] = (name: string, ...args: unknown[]) =>
libs.add(name) && load().then(() => maps[L](name, ...args));
}
})({ key: apiKey });
}
// Custom hook to load the Woosmap JavaScript API via the loader shim.
function useWoosmap(apiKey: string) {
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
installLoaderShim(apiKey);
woosmap.map.importLibrary("maps").then(() => setIsLoaded(true));
}, [apiKey]);
return isLoaded;
}
// Contexts for Woosmap library
const MapContext = createContext<typeof woosmap | null>(null);
interface WoosmapAPIProviderProps {
apiKey: string;
children: React.ReactNode;
}
// This component uses the useWoosmap hook to load the Woosmap API and provides it through context.
const WoosmapAPIProvider: React.FC<WoosmapAPIProviderProps> = ({
apiKey,
children,
}) => {
const isLoaded = useWoosmap(apiKey);
if (!isLoaded) {
return null;
}
return <MapContext.Provider value={woosmap}>{children}</MapContext.Provider>;
};
// This component creates a Woosmap map and provides it through context used by the <Marker/>
const WoosmapMap: React.FC<woosmap.map.MapOptions> = ({ center, zoom }) => {
const mapRef = useRef<HTMLDivElement>(null);
const woosmap = useContext(MapContext);
const [mapInstance, setMapInstance] = useState<woosmap.map.Map>(null);
useEffect(() => {
if (mapRef.current && !mapInstance) {
const map = new woosmap.map.Map(mapRef.current, {
zoom,
center,
});
setMapInstance(map);
}
}, [woosmap, zoom, center]);
return <div ref={mapRef} style={{ width: "100%", height: "100vh" }}></div>;
};
const App: React.VFC = () => {
const initialPosition: woosmap.map.LatLngLiteral = {
lat: 50.2176,
lng: -0.8997,
};
return (
<WoosmapAPIProvider apiKey={"YOUR_API_KEY"}>
<WoosmapMap center={initialPosition} zoom={4}></WoosmapMap>
</WoosmapAPIProvider>
);
};
window.addEventListener("DOMContentLoaded", () => {
const root = createRoot(document.getElementById("root")!);
root.render(<App />);
});
import { createRoot } from "react-dom/client";
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from "react";
const WOOSMAP_SDK_HOST = "https://sdk.woosmap.com";
// Installs the documented Woosmap loader shim (see docs/docs/loader.md), which
// defines a stub `woosmap.map.importLibrary` that fetches map.js on first call.
// Safe to call more than once: the shim itself no-ops (and warns) past the first.
function installLoaderShim(apiKey) {
((g) => {
let p;
const w = window,
d = document;
const L = "importLibrary",
CB = "__ib__";
const E = "The Woosmap Maps JavaScript API";
const woosmap = w.woosmap || (w.woosmap = {});
const maps = woosmap.map || (woosmap.map = {});
const libs = new Set(),
params = new URLSearchParams();
const load = () =>
p ||
(p = new Promise((res, rej) => {
const s = d.createElement("script");
params.set("libraries", [...libs].join(","));
for (const k in g) params.set(k, g[k]);
params.set("callback", "woosmap.map." + CB);
s.src = `${WOOSMAP_SDK_HOST}/map/map.js?` + params;
maps[CB] = res;
s.onerror = () => rej(Error(E + " could not load."));
d.head.append(s);
}));
if (maps[L]) {
console.warn(E + " only loads once. Ignoring:", g);
} else {
maps[L] = (name, ...args) =>
libs.add(name) && load().then(() => maps[L](name, ...args));
}
})({ key: apiKey });
}
// Custom hook to load the Woosmap JavaScript API via the loader shim.
function useWoosmap(apiKey) {
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
installLoaderShim(apiKey);
woosmap.map.importLibrary("maps").then(() => setIsLoaded(true));
}, [apiKey]);
return isLoaded;
}
// Contexts for Woosmap library
const MapContext = createContext(null);
// This component uses the useWoosmap hook to load the Woosmap API and provides it through context.
const WoosmapAPIProvider = ({ apiKey, children }) => {
const isLoaded = useWoosmap(apiKey);
if (!isLoaded) {
return null;
}
return <MapContext.Provider value={woosmap}>{children}</MapContext.Provider>;
};
// This component creates a Woosmap map and provides it through context used by the <Marker/>
const WoosmapMap = ({ center, zoom }) => {
const mapRef = useRef(null);
const woosmap = useContext(MapContext);
const [mapInstance, setMapInstance] = useState(null);
useEffect(() => {
if (mapRef.current && !mapInstance) {
const map = new woosmap.map.Map(mapRef.current, {
zoom,
center,
});
setMapInstance(map);
}
}, [woosmap, zoom, center]);
return <div ref={mapRef} style={{ width: "100%", height: "100vh" }}></div>;
};
const App = () => {
const initialPosition = {
lat: 50.2176,
lng: -0.8997,
};
return (
<WoosmapAPIProvider apiKey={"YOUR_API_KEY"}>
<WoosmapMap center={initialPosition} zoom={4}></WoosmapMap>
</WoosmapAPIProvider>
);
};
window.addEventListener("DOMContentLoaded", () => {
const root = createRoot(document.getElementById("root"));
root.render(<App />);
});
/*
* Always set the map height explicitly to define the size of the element
* that contains the map. `display: block` is required for <wmp-map>/<gmp-map>,
* which default to inline like any other custom element outside a flex/grid
* container (no-op for a plain <div>).
*/
#map {
display: block;
width: 100%;
height: 100%;
}
/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}
<html>
<head>
<title>React Map Basic</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
Running the Sample Locally
Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow these instructions to get them set up.
Once you have Git and Node.js installed, you can run the sample by following these steps:
-
Clone the repository and navigate to the directory of the sample.
-
Install the necessary dependencies.
-
Start running the sample.
Here are the commands you can use in your terminal to do this:
git clone -b sample/react-map-basic https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start
You can experiment with other samples by switching to any branch that starts with the pattern sample/SAMPLE_NAME.
git checkout sample/SAMPLE_NAME
npm i
npm start