GeoJSON layer
A GeoJSON file as the map's layer: points as markers, a line, an area, each with a popup.
Map and code
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends
Fetches regents-park.json
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-maplibre-map { height: 36rem; }
#geojson-maplibre-map > .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<!-- A GeoJSON file as the map's layer: the park an area, its avenue a line, its places markers, each feature's about property in the popup a click opens. -->
<maplace-map id="geojson-maplibre-map" list></maplace-map>
<script type="module">
const response = await fetch("/examples/data/regents-park.json");
document.getElementById("geojson-maplibre-map").locations = Maplace.locationsFromGeoJSON(await response.json()).map(
(feature) => ({ ...feature, content: feature.data?.about }),
);
</script><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-map { height: 36rem; }
#geojson-map > .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<!-- A GeoJSON file as the map's layer: the park an area, its avenue a line, its places markers, each feature's about property in the popup a click opens. -->
<maplace-map id="geojson-map" api-key="YOUR_KEY" list></maplace-map>
<script type="module">
const response = await fetch("/examples/data/regents-park.json");
document.getElementById("geojson-map").locations = Maplace.locationsFromGeoJSON(await response.json()).map(
(feature) => ({ ...feature, content: feature.data?.about }),
);
</script><!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet, what the map and its list mount on, and the map made with Maplace; the layer from a GeoJSON file. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#geojson-maplibre-script-map-and-list { display: flex; height: 24rem; }
#geojson-maplibre-script-map { flex: 1 1 auto; min-width: 0; }
#geojson-maplibre-script-list { display: contents; }
#geojson-maplibre-script-list .maplace-list-view { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-maplibre-script-map-and-list { flex-direction: column; height: 36rem; }
#geojson-maplibre-script-list .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="geojson-maplibre-script-map-and-list">
<div id="geojson-maplibre-script-map"></div>
<div id="geojson-maplibre-script-list"></div>
</div>
<script type="module">
const maplace = Maplace.createMapLibreMap("#geojson-maplibre-script-map", {
list: { container: "#geojson-maplibre-script-list" },
});
// Each feature a location: a Point a marker, a LineString a line, a Polygon an area; its
// about property in the popup a click opens.
const response = await fetch("/examples/data/regents-park.json");
maplace.setLocations(
Maplace.locationsFromGeoJSON(await response.json()).map((feature) => ({
...feature,
content: feature.data?.about,
})),
);
</script><!doctype html>
<!-- A page of your own script: the CDN script and what the map and its list mount on, and the map made with Maplace, which adds Maps JS with your key; the layer from a GeoJSON file. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#geojson-script-map-and-list { display: flex; height: 24rem; }
#geojson-script-map { flex: 1 1 auto; min-width: 0; }
#geojson-script-list { display: contents; }
#geojson-script-list .maplace-list-view { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-script-map-and-list { flex-direction: column; height: 36rem; }
#geojson-script-list .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="geojson-script-map-and-list">
<div id="geojson-script-map"></div>
<div id="geojson-script-list"></div>
</div>
<script type="module">
const maplace = Maplace.createGoogleMap("#geojson-script-map", {
apiKey: "YOUR_KEY",
list: { container: "#geojson-script-list" },
});
// Each feature a location: a Point a marker, a LineString a line, a Polygon an area; its
// about property in the popup a click opens.
const response = await fetch("/examples/data/regents-park.json");
maplace.setLocations(
Maplace.locationsFromGeoJSON(await response.json()).map((feature) => ({
...feature,
content: feature.data?.about,
})),
);
</script><!doctype html>
<!-- The page vanilla.ts runs on: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#geojson-maplibre-vanilla-map-and-list { display: flex; height: 24rem; }
#geojson-maplibre-vanilla-map { flex: 1 1 auto; min-width: 0; }
#geojson-maplibre-vanilla-list { display: contents; }
#geojson-maplibre-vanilla-list .maplace-list-view { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-maplibre-vanilla-map-and-list { flex-direction: column; height: 36rem; }
#geojson-maplibre-vanilla-list .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="geojson-maplibre-vanilla-map-and-list">
<div id="geojson-maplibre-vanilla-map"></div>
<div id="geojson-maplibre-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { locationsFromGeoJSON } from "@maplacejs/maplace";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";
const maplace = createMapLibreMap<{ about: string }>("#geojson-maplibre-vanilla-map", {
list: { container: "#geojson-maplibre-vanilla-list" },
});
// Each feature a location: a Point a marker, a LineString a line, a Polygon an area; its about
// property in the popup a click opens.
const response = await fetch("/examples/data/regents-park.json");
const features = locationsFromGeoJSON<{ about: string }>(await response.json());
maplace.setLocations(features.map((feature) => ({ ...feature, content: feature.data?.about })));<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->Google's bootstrap loader, with your API key: from Google's docs
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "YOUR_KEY",
v: "weekly",
});
</script><style>
#geojson-vanilla-map-and-list { display: flex; height: 24rem; }
#geojson-vanilla-map { flex: 1 1 auto; min-width: 0; }
#geojson-vanilla-list { display: contents; }
#geojson-vanilla-list .maplace-list-view { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-vanilla-map-and-list { flex-direction: column; height: 36rem; }
#geojson-vanilla-list .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="geojson-vanilla-map-and-list">
<div id="geojson-vanilla-map"></div>
<div id="geojson-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import { locationsFromGeoJSON } from "@maplacejs/maplace";
import { createGoogleMap } from "@maplacejs/maplace/google";
const maplace = createGoogleMap<{ about: string }>("#geojson-vanilla-map", {
list: { container: "#geojson-vanilla-list" },
});
// Each feature a location: a Point a marker, a LineString a line, a Polygon an area; its about
// property in the popup a click opens.
const response = await fetch("/examples/data/regents-park.json");
const features = locationsFromGeoJSON<{ about: string }>(await response.json());
maplace.setLocations(features.map((feature) => ({ ...feature, content: feature.data?.about })));<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#geojson-maplibre-react-root .map-and-list { display: flex; height: 24rem; }
#geojson-maplibre-react-root .map { flex: 1 1 auto; min-width: 0; }
#geojson-maplibre-react-root .maplace-list-view { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-maplibre-react-root .map-and-list { flex-direction: column; height: 36rem; }
#geojson-maplibre-react-root .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="geojson-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { type Location, locationsFromGeoJSON } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
function GeoJsonLayer() {
const [features, setFeatures] = useState<readonly Location<{ about: string }>[]>([]);
useEffect(() => {
// Each feature a location: a Point a marker, a LineString a line, a Polygon an area.
fetch("/examples/data/regents-park.json")
.then((response) => response.json())
.then((collection) => setFeatures(locationsFromGeoJSON<{ about: string }>(collection)));
}, []);
return (
<MaplaceProvider locations={features}>
<div className="map-and-list">
<MaplaceMap
createAdapter={createMapLibreAdapter}
className="map"
// Each feature's about property, in the popup a click opens.
popupContent={({ data }: Location<{ about: string }>) => data?.about}
/>
<LocationList />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("geojson-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<GeoJsonLayer />
</StrictMode>,
);<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->Google's bootstrap loader, with your API key: from Google's docs
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "YOUR_KEY",
v: "weekly",
});
</script><style>
#geojson-react-root .map-and-list { display: flex; height: 24rem; }
#geojson-react-root .map { flex: 1 1 auto; min-width: 0; }
#geojson-react-root .maplace-list-view { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#geojson-react-root .map-and-list { flex-direction: column; height: 36rem; }
#geojson-react-root .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="geojson-react-root"></div>
<script type="module" src="./react.tsx"></script>import { type Location, locationsFromGeoJSON } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
function GeoJsonLayer() {
const [features, setFeatures] = useState<readonly Location<{ about: string }>[]>([]);
useEffect(() => {
// Each feature a location: a Point a marker, a LineString a line, a Polygon an area.
fetch("/examples/data/regents-park.json")
.then((response) => response.json())
.then((collection) => setFeatures(locationsFromGeoJSON<{ about: string }>(collection)));
}, []);
return (
<MaplaceProvider locations={features}>
<div className="map-and-list">
<MaplaceMap
createAdapter={createGoogleAdapter}
className="map"
// Each feature's about property, in the popup a click opens.
popupContent={({ data }: Location<{ about: string }>) => data?.about}
/>
<LocationList />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("geojson-react-root") as HTMLElement).render(
<StrictMode>
<GeoJsonLayer />
</StrictMode>,
);How it works
A GeoJSON file drawn as the map’s layer: the page fetches regents-park.json, a FeatureCollection, and locationsFromGeoJSON turns each feature into a location. The park’s Polygon becomes an area, the Broad Walk’s LineString a line, and four Points markers. Each feature’s id is its location’s, its title and group properties name it and head it in the list, and its about property goes in the popup a click opens.
- HTML:
Maplace.locationsFromGeoJSONon the CDN script, its result, each feature’saboutcopied to itscontent, set as the map’slocationsproperty (Properties and methods). - Script and Bundler:
locationsFromGeoJSON, from the core entry, each feature’saboutcopied to itscontent, the result given to the store’ssetLocations(Methods). - React: the same call once the file arrives, its result kept in state and given to
<MaplaceProvider locations>(<MaplaceProvider>), andpopupContenton<MaplaceMap>writes each feature’saboutin its popup (<MaplaceMap>). - Note: a file without ids, or with geometries the helper doesn’t read (
MultiPoint,MultiLineString), is mapped first; From GeoJSON shows how.