Data displayMapView
Searchastronomic.dev# MapView
For homes or places on a map with price pins that open a small card, loaded only when it comes into view.
Interactive: inline script.
## Usage
```astro
import MapView from "@astronomic/ui-pro/components/MapView.astro";
```
## Examples
### Price pins
Eight homes in Denver. Each pin shows its price (rentals in the second color) and opens a small card. The map loads only when it comes into view.
```astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listings and fields. Each point is what its pin and card show, and its place.
import { fields } from "../_data/vega.ts";
import { denver, exampleStyle, points } from "../_data/map.ts";
---
<!-- A map of listings: price pins (rentals in the second color) that open a small card. It loads when it comes into view. -->
<MapView label="Map of homes" center={denver} zoom={11} fields={fields} points={points} mapStyle={exampleStyle} clusterLabel="{count} homes here. Zoom in." failedText="The map is not available. Every home is listed on this page." />
```
### One home
One point, as on a home's own page: the map centers on it at street zoom.
```astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listing: one point, as on a home's own page.
import { fields, listings } from "../_data/vega.ts";
import { exampleStyle, toPoint } from "../_data/map.ts";
const home = listings[0];
---
<!-- One home: the map centers on its pin at street zoom. -->
<MapView label={`Map of ${home.address}`} center={home.point} zoom={14} fields={fields} points={[toPoint(home)]} mapStyle={exampleStyle} class="max-w-xl" />
```
### Our own map style
The default style: streets, water, parks and names in the theme's own colors, read when the map loads. On a site it follows light and dark mode as they change.
```astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listings and fields. Each point is what its pin and card show, and its place.
import { fields } from "../_data/vega.ts";
import { denver, points } from "../_data/map.ts";
---
<!-- Our own map style, the default: OpenFreeMap's tiles in the theme's colors. It follows the preset and the mode. -->
<MapView label="Map of homes" center={denver} zoom={12} fields={fields} points={points} clusterLabel="{count} homes here. Zoom in." failedText="The map is not available. Every home is listed on this page." />
```
### When the map cannot load
No WebGL, a failed download, or a style that errors: the note shows in the map's place.
```astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listings and fields.
import { fields } from "../_data/vega.ts";
import { denver, points } from "../_data/map.ts";
---
<!-- A style that does not load: the note shows in the map's place, and the page around it is untouched. -->
<MapView label="Map of homes" center={denver} fields={fields} points={points} mapStyle="/no-such-map-style.json" failedText="The map is not available right now. Every home is listed on this page." />
```
## Props
### MapView
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | None | The map region's name, such as "Map of homes". |
| `center` | `{ lat: number; lng: number }` | None | Where the map opens before it fits the points. |
| `zoom` | `number` | `11` | The zoom it opens at. One point opens at street zoom (14) or more. |
| `fields` | `ListingField[]` | None | The fields list (@astronomic-dev/ui/lib/listing.ts): the pin card's facts line shows each card field with a shortLabel. |
| `points` | `MapPoint[]` | `[]` | The pins: each one's card data (id, href and title; the rest optional) and its lat and lng. Rentals (a price per month) take the second pin color. |
| `mapStyle` | `"astronomic" \| "liberty" \| "bright" \| "positron" \| "dark" \| "fiord" \| string` | `"astronomic"` | "astronomic" is our own style: OpenFreeMap's tiles in the theme's colors, read from its tokens, light and dark, with names at 4.5:1 or more. Set --ui-map-land, --ui-map-water, --ui-map-park, --ui-map-building, --ui-map-road or --ui-map-label to take over one color. Or name an OpenFreeMap style (no key needed), or pass a style URL. |
| `resultsId` | `string` | None | The id of the ListingResults the map follows. Set by MapSplit. |
| `skipToId` | `string` | None | The id "Skip the map" goes to: a heading or box with tabindex="-1". Required with resultsId; without it, no skip link. |
| `skipText` | `string` | `"Skip the map"` | The skip link's text. |
| `clusterLabel` | `string` | `"{count} listings here. Zoom in."` | A cluster's name; {count} is filled in. |
| `failedText` | `string` | `"The map is not available. The list shows every listing."` | The note shown when the map cannot load. |
| `linkText` | `string` | `"See the listing"` | The pin card's action. |
| `closeLabel` | `string` | `"Close"` | The pin card's close button name. |
| `frameClass` | `string` | None | For recipes. Classes for the map's box: its size (4:3, 16:9 from a 48rem container) and corners. |
| `class` | `string` | None | More classes for the <section>, merged with cn(), so yours win on conflicts. |
Any other attribute goes on the <section>.
## Accessibility
| Key | Action |
| --- | --- |
| Tab | Moves from Skip the map to the map, its zoom buttons, the clusters and the pins |
| Enter or Space on a pin | Opens its card and moves focus to the card, with no ring. The next Tab goes to Close, then the title link |
| Escape in the card | Closes the card and returns focus to its pin |
| Enter or Space on a cluster | Zooms in until its listings split apart |
| Arrow keys and + or - on the map | Pan and zoom the map (MapLibre's own keys) |
- The map is a region named by label. "Skip the map" comes first, shows when it has focus, and moves focus to skipToId (give that heading tabindex="-1").
- Each pin is a button named by the full price and the address ("$1,125,000, 3721 Grove St"), with aria-expanded and aria-controls naming its card. One card is open at a time; it has a Close button first, then the listing's link.
- A cluster is a button named by clusterLabel ("14 listings here. Zoom in.").
- When the pin with focus or an open card joins a cluster as you zoom out, or leaves the map, focus moves to the cluster that holds it, else to the map's region, never to the page's start.
- While MapLibre and the style load, the map box is aria-busy. If the map cannot load (no WebGL, a failed download, a style that errors or takes over 8 seconds, a worker that cannot load), the note shows in its place. It takes focus only when the reader asked for the map (Show map); a map that fails as it scrolls into view stays quiet.
- Pins, clusters, the zoom buttons and the card's Close are at least 44px on a touch screen. The attribution stays visible, as OpenFreeMap and OpenStreetMap require.
- Under reduced motion or the theme's calm setting the map jumps instead of glides, and pins do not grow.
MapView
ProFor homes or places on a map with price pins that open a small card, loaded only when it comes into view.
- File
- MapView.astro
- Group
- Data display
- Kind
- Interactive: inline script
- Examples
- 4
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
Price pins
Eight homes in Denver. Each pin shows its price (rentals in the second color) and opens a small card. The map loads only when it comes into view.
The map is not available. Every home is listed on this page.
examples/mapview/main.astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listings and fields. Each point is what its pin and card show, and its place.
import { fields } from "../_data/vega.ts";
import { denver, exampleStyle, points } from "../_data/map.ts";
---
<!-- A map of listings: price pins (rentals in the second color) that open a small card. It loads when it comes into view. -->
<MapView label="Map of homes" center={denver} zoom={11} fields={fields} points={points} mapStyle={exampleStyle} clusterLabel="{count} homes here. Zoom in." failedText="The map is not available. Every home is listed on this page." />One home
One point, as on a home's own page: the map centers on it at street zoom.
The map is not available. The list shows every listing.
examples/mapview/one-home.astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listing: one point, as on a home's own page.
import { fields, listings } from "../_data/vega.ts";
import { exampleStyle, toPoint } from "../_data/map.ts";
const home = listings[0];
---
<!-- One home: the map centers on its pin at street zoom. -->
<MapView label={`Map of ${home.address}`} center={home.point} zoom={14} fields={fields} points={[toPoint(home)]} mapStyle={exampleStyle} class="max-w-xl" />Our own map style
The default style: streets, water, parks and names in the theme's own colors, read when the map loads. On a site it follows light and dark mode as they change.
The map is not available. Every home is listed on this page.
examples/mapview/astronomic-style.astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listings and fields. Each point is what its pin and card show, and its place.
import { fields } from "../_data/vega.ts";
import { denver, points } from "../_data/map.ts";
---
<!-- Our own map style, the default: OpenFreeMap's tiles in the theme's colors. It follows the preset and the mode. -->
<MapView label="Map of homes" center={denver} zoom={12} fields={fields} points={points} clusterLabel="{count} homes here. Zoom in." failedText="The map is not available. Every home is listed on this page." />When the map cannot load
No WebGL, a failed download, or a style that errors: the note shows in the map's place.
The map is not available right now. Every home is listed on this page.
examples/mapview/failed.astro
---
import MapView from "@astronomic/ui-pro/components/MapView.astro";
// Your own listings and fields.
import { fields } from "../_data/vega.ts";
import { denver, points } from "../_data/map.ts";
---
<!-- A style that does not load: the note shows in the map's place, and the page around it is untouched. -->
<MapView label="Map of homes" center={denver} fields={fields} points={points} mapStyle="/no-such-map-style.json" failedText="The map is not available right now. Every home is listed on this page." />