Minimal
Here is a minimal map from openstreetmap, in a single line component call: <Leaflet />
Full code
Code Snippet
---import { Leaflet } from "astro-leaflet";---<Leaflet />
npm install astro-leaflet
Read the full doc on Github
Leaflet documentation can be found on leafletjs.com
In case of any problem (map not displayed,…), please refer to the Troubleshooting section
Minimal
Here is a minimal map from openstreetmap, in a single line component call: <Leaflet />
Full code
Code Snippet
---import { Leaflet } from "astro-leaflet";---<Leaflet />
Google Map with Markers
An hybrid map (satellite, with country names, towns…) from Google is displayed, along with 2 markers (default leaflet marker on Paris, and custom marker on New York).
The hybrid map is selected using
(please check following section to list and display possible values of tileByName
)
<Leaflet options={{ tileByName: 'Google&type=hybrid', }}/>
which is the same as
<Leaflet options={{ tileLayer: 'https://{s}.google.com/vt/lyrs=y&hl=en&x={x}&y={y}&z={z}', tileLayerOptions: { subdomains: [ 'mt0', 'mt1', 'mt2', 'mt3' ], attribution: 'Map data © Google', },}}/>
The custom icon is created using the following, along with the definition
of the css class myicon-name
<CreateLeafletIcon name='myicon-name' options={{className:"leaflet-icon-myicon", iconSize: [20,20]}}/>
And the markers are added using <Leaflet/>
markers
markers = [ { latlng: [48.8575299,2.3509908], options: {title: 'Paris'} }, { latlng: [40.7194951,-73.9985291], options: {title: 'New York', }, astroIconName:'myicon-name'}]
Full code
Code Snippet
---import { Leaflet, CreateLeafletIcon } from "astro-leaflet";---<CreateLeafletIcon name='myicon-name' options={{className:"leaflet-icon-myicon", iconSize: [20,20]}}/><LeaflettileByName: 'Google&type=hybrid',markers: {[ { latlng: [48.8575299,2.3509908], options: {title: 'Paris'}}, { latlng: [40.7194951,-73.9985291], options: {title: 'New York', }, astroIconName:'myicon-name'}]}/>
<style is:global>/* class definition used to define the custom icon */.leaflet-icon-myicon { background:red; border:5px solid rgba(255,255,255,0.5); border-radius:50%;}</style>
Select Layer
Here is the map friendly name list to be given in tileByName0
argument
of LeafLet
and TileLayer
components.
Click on the name to see how it renders, its description
as well as the parameters to drive it, and how to call it in astro-leaflet.
NYC Marathon
This demonstrates:
<TileLayer>
inside <LeafLet>
<Polyline>
<FitBounds />
inside <Polyline>
Full code
Code Snippet
---import { Leaflet, TileLayer, Polyline, FitBounds} from "astro-leaflet";import type { LatLngTuple } from 'leaflet'
// load marathon data: list of [lat lng]import data from './new-york-city-marathon.json'---
<Leaflet options={{ options={{tileByName:'Google'}} }}><TileLayer tileByName='Michelin&type=label', options={{ ... }} /><Polyline latlngs={data as LatLngTuple[]} options={{color: 'red', weight: 4,}} > <FitBounds /></Polyline></Leaflet>
Image Overlay
Leaflet Quick Start Guide
Leaflet Tutorial implemented with astro-leaflet. This will quickly get you started on Leaflet basics, including setting up a Leaflet map, working with markers, polylines and popups, and dealing with events.
Full code
Highlights:
\<Leaflet>
is used with
id="map-quick-start"
to style the height of the map
in the \<style is:global>
sectionstyle=""
is provided.
This is because a default map size is provided when neither style nor class is given.center
and zoom
are provided\<Marker>
, \<Circle>
and \<Polygon>
are given along with \<Popup>
\<Popup>
is given with latlng.Dealing with event must be performed in the \<script>
part:
load
event is fired, we are ensured all leaflet elements are created,
including the map. The map can be retrieved using its id by calling
const map = getMapFromId('map-quick-start')
Leaflet on Mobile
Leaflet Tutorial implemented with astro-leaflet. Create a map tuned, and how to easily detect and use the current user location
Full code
Highlights:
\<Leaflet>
with an id to be able to get the associated map
into the script part/<FitWorld>
and /<Locate>
inside \<Leaflet>
\<script>
part:
locationfound
and locationerror
Interactive Choropleth Map
Leaflet Tutorial implemented with astro-leaflet. This is a case study of creating a colorful interactive choropleth map of US States Population Density with the help of GeoJSON and some custom controls (that will hopefully convince all the remaining major news and government websites that do not use Leaflet yet to start doing so).
Full code
Highlights:
\<GeoJson>
to have US states overlayed on the map\<Control>
to display legend, and information on the hovered stateLayer Groups and Layers Control
Leaflet Tutorial implemented with astro-leaflet. It will show you how to group several layers into one, and how to use the layers control to allow users to easily switch different layers on your map
Full code
Highlights:
\<Leaflet>
with noDefault={true}
so that no default layer is added onto the map\<ControlLayer>
to select the layers to be displayed,
and switch from one to another\<BaseLayer>
to add a \<TileLayer>
onto the control as a checkbox\<LayerGroup>
to group \<Marker>
, to be able to select them all in once\<Overlay>
to add a \<LayerGroup>
onto the control as a radioTroubleshooting
If the map is not displayed, and you have the following error in the console
Uncaught SyntaxError: The requested module '/node_modules/leaflet/dist/leaflet-src.js?v=c7414b9d' does not provide an export named 'layerGroup' (at layergroup.ts?v=c7414b9d:1:10)
or
SyntaxError: Importing binding name 'default' cannot be resolved by star
then you have to add the following inside astro.config.mjs
:
export default defineConfig({ ... vite: { optimizeDeps: { include: ['leaflet'], } }, ...});
Test Your Url
Provide your favorite tile url, and see how it renders.