MapLibre map with MDX

Published on: Wed Jun 07 2023


Research

The research question for static maps has come up in the context of blog posts.

Solution

An Astro component based on MapLibre GL JS.

  1. Import MapLibre GL JS via package.json
  2. Create an Astro component, which exposes the parameters to pass into the component: latitude, longitude, zoom, style, etc

MDX code

import MapLibre from "~/components/MapLibre.astro"

export const latitude = 32.870381
export const longitude = -116.98891
export const zoom = 3
export var mapstyle1 = "https://demotiles.maplibre.org/style.json"
export var mapstyle2 = "https://raw.githubusercontent.com/roblabs/xyz-raster-sources/master/styles/park-tiles-field-mapper.json"
export var mapstyle3 = "https://zelonewolf.github.io/openstreetmap-americana/style.json"
export var mapstyle4 = "https://roblabs.com/xyz-raster-sources/styles/openstreetmap.json"
export var mapstyle5 = "https://roblabs.com/xyz-raster-sources/styles/naturalearthtiles.json"
export var mapstyle6 = "https://roblabs.com/xyz-raster-sources/styles/arcgis-world-imagery.json"
export var mapstyle7 = "https://atlas-stg.geoplatform.gov/styles/v1/atlas-user/ck58pyquo009v01p99xebegr9?access_token=pk.eyJ1IjoiYXRsYXMtdXNlciIsImEiOiJjazFmdGx2bjQwMDAwMG5wZmYwbmJwbmE2In0.lWXK2UexpXuyVitesLdwUg"
export var mapstyle = mapstyle3

MapLibre Map

This section prints to screen the exported values passed into the map

Example - use set variables

<MapLibre
  container="mapdiv-with-exported-variables"
  latitude={latitude}
  longitude={longitude}
  zoom={zoom}
  mapstyle={mapstyle}
/>

Example - with variables in the component

Astro components look like HTML, so putting quotes around Numbers are required

<MapLibre
  container="mapdiv-with-variables-in-the-component"
  latitude="32.0"
  longitude="-116.0"
  zoom="3"
  mapstyle="https://demotiles.maplibre.org/style.json"
/>

<div> is mapdiv-with-variables-in-the-component


Example - interactive is true

<MapLibre
  container="mapdiv-with-variables-in-the-component-interactive-true"
  latitude="32.0"
  longitude="-116.0"
  zoom="3"
  mapstyle="https://demotiles.maplibre.org/style.json"
  interactive="true"
/>

<div> is mapdiv-with-variables-in-the-component-interactive-true


Example - Park Tiles Raster

<MapLibre
  container="mapdiv-park-tiles-raster-interactive-false"
  latitude="32.0"
  longitude="-116.0"
  zoom="3"
  mapstyle="https://raw.githubusercontent.com/roblabs/xyz-raster-sources/master/styles/park-tiles-field-mapper.json"
/>

<div> is container="mapdiv-park-tiles-raster-interactive-false"


🚀 Features

Roadmap Features