Map Grid
terrae/gridFreeComponent
Coordinate grid overlay with latitude/longitude lines and labels.
Install it
npx shadcn@latest add https://www.terrae.dev/grid.jsonSource
1"use client"23import { useEffect, useRef } from "react"4import { createPortal } from "react-dom"5import { useMap } from "./hooks"67type GridLabel = {8 id: string9 text: string10 position: { x: number; y: number }11 type: "lat" | "lng"12}1314type GridLinesResult = {15 latitudeLines: GeoJSON.Feature<GeoJSON.LineString>[]16 longitudeLines: GeoJSON.Feature<GeoJSON.LineString>[]17 labels: GridLabel[]18}1920type MapGridProps = {21 id?: string22 latitudeInterval?: number23 longitudeInterval?: number24 lineColor?: string25 lineOpacity?: number26 lineWidth?: number27 showLabels?: boolean28 labelColor?: string29 labelSize?: number30 labelBackground?: string31}3233const DEFAULT_ID = "map-grid"34const DEFAULT_LATITUDE_INTERVAL = 1035const DEFAULT_LONGITUDE_INTERVAL = 1036const DEFAULT_LINE_COLOR = "#ffffff"37const DEFAULT_LINE_OPACITY = 0.338const DEFAULT_LINE_WIDTH = 139const DEFAULT_LABEL_COLOR = "#ffffff"40const DEFAULT_LABEL_SIZE = 1041const DEFAULT_LABEL_BACKGROUND = "rgba(0, 0, 0, 0.5)"4243const MIN_LATITUDE = -9044const MAX_LATITUDE = 9045const MIN_RENDER_LATITUDE = -8546const MAX_RENDER_LATITUDE = 8547const LONGITUDE_WRAP = 18048const LONGITUDE_CYCLE = 3604950const LABEL_OFFSET_X = 851const LABEL_OFFSET_Y = 1652const LABEL_PADDING = "2px 4px"53const LABEL_BORDER_RADIUS = "2px"5455const createLatitudeFeature = (latitude: number, west: number, east: number): GeoJSON.Feature<GeoJSON.LineString> => {56 return {57 type: "Feature",58 properties: { latitude },59 geometry: {60 type: "LineString",61 coordinates: [62 [west, latitude],63 [east, latitude],64 ],65 },66 }67}6869const createLongitudeFeature = (70 longitude: number,71 south: number,72 north: number73): GeoJSON.Feature<GeoJSON.LineString> => {74 const normalizedLongitude =75 ((((longitude + LONGITUDE_WRAP) % LONGITUDE_CYCLE) + LONGITUDE_CYCLE) % LONGITUDE_CYCLE) - LONGITUDE_WRAP7677 return {78 type: "Feature",79 properties: { longitude: normalizedLongitude },80 geometry: {81 type: "LineString",82 coordinates: [83 [longitude, Math.max(south, MIN_RENDER_LATITUDE)],84 [longitude, Math.min(north, MAX_RENDER_LATITUDE)],85 ],86 },87 }88}8990const createLatitudeLabel = (latitude: number, west: number, map: mapboxgl.Map): GridLabel => {91 const point = map.project([west, latitude])92 const direction = latitude >= 0 ? "N" : "S"9394 return {95 id: `lat-${latitude}`,96 text: `${Math.abs(latitude)}°${direction}`,97 position: { x: point.x + LABEL_OFFSET_X, y: point.y },98 type: "lat",99// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from terrae
All 40 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Map Animated Circleanimated-circle | terrae | Components | Free | 1 dep | |
| Map Animated Footprintanimated-footprint | terrae | Components | Free | 2 deps | |
| Map Animated Polygonanimated-polygon | terrae | Components | Free | 1 dep | |
| Map Animated Pulseanimated-pulse | terrae | Components | Free | 1 dep | |
| Map Arc Animatedarc-animated | terrae | Components | Free | 1 dep | |
| Map Blur Areablur-area | terrae | Components | Free | 1 dep | |
| Map Camera Followcamera-follow | terrae | Components | Free | 1 dep | |
| Map Choroplethchoropleth | terrae | Components | Free | 1 dep |
