Maplace.js

Live data and loading

GeoJSON layer

A GeoJSON file as the map's layer: points as markers, a line, an area, each with a popup.

Markdown

Map and code

Run the examples on

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>

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.locationsFromGeoJSON on the CDN script, its result, each feature’s about copied to its content, set as the map’s locations property (Properties and methods).
  • Script and Bundler: locationsFromGeoJSON, from the core entry, each feature’s about copied to its content, the result given to the store’s setLocations (Methods).
  • React: the same call once the file arrives, its result kept in state and given to <MaplaceProvider locations> (<MaplaceProvider>), and popupContent on <MaplaceMap> writes each feature’s about in 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.