Polygon

Storybook

Open the Polygon story

Polygon draws a google.maps.Polygon. It accepts a single ring or several rings (outer boundary first, then holes). With editable or draggable, edits flow back through bind:paths.

Props

PropTypeDefaultDescription
pathsgoogle.maps.LatLngLiteral[] | google.maps.LatLngLiteral[][]One ring, or an array of rings. Bindable: use bind:paths to track edits.
strokeColorstringStroke color.
strokeOpacitynumberStroke opacity between 0 and 1.
strokeWeightnumberStroke width in pixels.
strokePositiongoogle.maps.StrokePositionStroke position (CENTER, INSIDE, OUTSIDE).
fillColorstringFill color.
fillOpacitynumberFill opacity between 0 and 1.
geodesicbooleanDraw great-circle edges.
clickablebooleanWhether the shape handles mouse events.
draggablebooleanLets the user drag the shape.
editablebooleanShows handles so the user can edit the shape.
visiblebooleanWhether the shape is visible.
zIndexnumberStacking order.
optionsgoogle.maps.PolygonOptionsOther polygon options. Individual props override these.
polygongoogle.maps.Polygon | nullnullThe instance. Bindable: read it with bind:polygon.

Event Props

PropTypeDescription
onClick(event: google.maps.PolyMouseEvent) => voidclick
onDblClick(event: google.maps.PolyMouseEvent) => voiddblclick
onContextMenu(event: google.maps.PolyMouseEvent) => voidcontextmenu
onMouseDown(event: google.maps.PolyMouseEvent) => voidmousedown
onMouseUp(event: google.maps.PolyMouseEvent) => voidmouseup
onMouseOver(event: google.maps.PolyMouseEvent) => voidmouseover
onMouseOut(event: google.maps.PolyMouseEvent) => voidmouseout
onMouseMove(event: google.maps.PolyMouseEvent) => voidmousemove
onDragStart(event: google.maps.MapMouseEvent) => voiddragstart
onDrag(event: google.maps.MapMouseEvent) => voiddrag
onDragEnd(event: google.maps.MapMouseEvent) => voiddragend
onPathsChanged(paths: google.maps.LatLngLiteral[][]) => voidCalled after the user edits or drags the polygon. Always receives an array of rings.
onLoad(polygon: google.maps.Polygon) => voidCalled once after the polygon is created.

Usage

<script lang="ts">
	import { APIProvider, GoogleMap, Polygon } from 'svelte-google-maps-api';

	let paths = $state([
		{ lat: 35.70, lng: 139.72 },
		{ lat: 35.70, lng: 139.80 },
		{ lat: 35.65, lng: 139.80 },
		{ lat: 35.65, lng: 139.72 }
	]);
</script>

<div style="height: 400px;">
	<APIProvider apiKey="YOUR_API_KEY">
		<GoogleMap center={{ lat: 35.68, lng: 139.76 }} zoom={12}>
			<Polygon bind:paths fillColor="#2563eb" fillOpacity={0.25} strokeColor="#2563eb" editable />
		</GoogleMap>
	</APIProvider>
</div>
svelte

Notes

  • With bind:paths, a single ring stays a single ring after edits. If you pass nested rings, or an edit produces more than one ring, the bound value is an array of rings.
  • onPathsChanged always receives an array of rings.