gistda-sphere-react

Polygon

A filled area, with holes, labels, editing, measurements and polygon operations

import { Polygon, SphereMap } from "gistda-sphere-react";

<SphereMap center={{ lon: 100.5, lat: 13.75 }} style={{ height: 400 }} zoom={11}>
  <Polygon
    positions={[
      { lon: 100.45, lat: 13.8 },
      { lon: 100.55, lat: 13.8 },
      { lon: 100.55, lat: 13.7 },
      { lon: 100.45, lat: 13.7 },
    ]}
    fillColor="rgba(255, 0, 0, 0.3)"
    lineColor="red"
    lineWidth={2}
    label="Zone A"
    title="Zone A"
  />
</SphereMap>

The shorter samples below leave out <SphereMap>, but every overlay goes inside one.

Props

PropTypeDescription
positionsLocation[] | GeoJSONRequired. Corners of the polygon (at least three). null entries start a hole. A GeoJSON Polygon or MultiPolygon (or a feature) also works.

Polygons also take the shape props below.

Shape props

All shapes (Polygon, Polyline, Circle, Rectangle and Dot) share these props. Props marked live update the shape in place, and changing any other prop re-creates it.

PropTypeDescription
lineColorstringLive. Outline color (any CSS color).
lineWidthnumberLive. Outline width in pixels.
lineStyle"Solid" | "Dashed" | "Dot" | number[]Live. Outline style, or a custom [dash, gap] pattern.
fillColorstringLive. Fill color, for areas.
titlestringPopup title.
detailstringPopup text.
popupPopupOptionsFull popup configuration.
popupContentReactNodeReact content for the popup.
labelstring | booleanText drawn on the shape, or true to use title.
labelOptionsMarkerOptionsOptions for the label marker.
editablebooleanLet users drag the corners (polygons and rectangles).
draggablebooleanLet users move the whole shape.
clickablebooleanShow a pointer cursor on hover.
pointerbooleanShow markers at the corners.
pivotLocationPoint used for the label and rotation.
visibleRangeRangeOnly show between these zoom levels.
zIndexnumberStacking order.
optionsRecord<string, unknown>Extra options for the Sphere constructor.
onClick(shape, location) => voidThe shape was clicked.
onHover / onLeave(shape) => voidThe pointer entered / left the shape.
onDrag / onDrop(shape) => voidThe shape is being dragged / was dropped.
refRefSee Ref.

Holes

Put null between the outer ring and each hole:

import { Polygon } from "gistda-sphere-react";

<Polygon
  positions={[
    { lon: 100.4, lat: 13.9 }, { lon: 100.7, lat: 13.9 },
    { lon: 100.7, lat: 13.6 }, { lon: 100.4, lat: 13.6 },
    null,
    { lon: 100.5, lat: 13.8 }, { lon: 100.6, lat: 13.8 },
    { lon: 100.6, lat: 13.7 }, { lon: 100.5, lat: 13.7 },
  ]}
/>

GeoJSON input

import { Polygon } from "gistda-sphere-react";

<Polygon
  positions={{
    type: "Polygon",
    coordinates: [[[100.45, 13.8], [100.55, 13.8], [100.55, 13.7], [100.45, 13.8]]],
  }}
  fillColor="rgba(0, 128, 255, 0.3)"
/>

To show whole GeoJSON documents (points, lines and areas together), use <GeoJson>.

Styling without flicker

Color, width and line style changes are applied with Sphere's update(), so a polygon can follow state without being redrawn:

import { Polygon } from "gistda-sphere-react";

<Polygon
  positions={zone}
  fillColor={hovered ? "rgba(255,0,0,.5)" : "rgba(255,0,0,.2)"}
  onHover={() => setHovered(true)}
  onLeave={() => setHovered(false)}
/>

Ref

import { Polygon, type PolygonRef } from "gistda-sphere-react";
import { useRef } from "react";

const zoneRef = useRef<PolygonRef>(null);

<Polygon ref={zoneRef} positions={zone} />

zoneRef.current?.getArea();     // 120000000 (square meters)
zoneRef.current?.getArea("en"); // "120 sq.km"
MethodDescription
getPolygon()The underlying sphere.Polygon.
getArea(language?)Area in square meters, or text such as "120 sq.km" with a language. For rai/ngan/wa, pass the area to formatThaiArea.
getBound(), getCentroid(), getPivot()Bounding box, center and label point.
union(other), intersection(other), difference(other), split(other)New polygons (not added to the map). See Spatial analysis.
contains(target), intersects(target), within(overlay), distanceTo(target, language?)Spatial tests against a location or another overlay.
rotate(angle), shift({ lon, lat })Rotate around the pivot, or move by an offset.
updateStyle(style)Change colors, width or line style imperatively.
togglePopup(show?, location?)Show, hide or toggle the popup.
toGeoJSON()The polygon as a GeoJSON feature.

On this page