Rectangle

Storybook

Open the Rectangle story

Rectangle draws a google.maps.Rectangle. With editable or draggable, the new bounds flow back through bind:bounds.

Props

PropTypeDefaultDescription
boundsgoogle.maps.LatLngBoundsLiteral | google.maps.LatLngBoundsRectangle bounds. Bindable.
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.
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.RectangleOptionsOther rectangle options. Individual props override these.
rectanglegoogle.maps.Rectangle | nullnullThe instance. Bindable: read it with bind:rectangle.

Event Props

PropTypeDescription
onClick(event: google.maps.MapMouseEvent) => voidclick
onDblClick(event: google.maps.MapMouseEvent) => voiddblclick
onContextMenu(event: google.maps.MapMouseEvent) => voidcontextmenu
onMouseDown(event: google.maps.MapMouseEvent) => voidmousedown
onMouseUp(event: google.maps.MapMouseEvent) => voidmouseup
onMouseOver(event: google.maps.MapMouseEvent) => voidmouseover
onMouseOut(event: google.maps.MapMouseEvent) => voidmouseout
onMouseMove(event: google.maps.MapMouseEvent) => voidmousemove
onDragStart(event: google.maps.MapMouseEvent) => voiddragstart
onDrag(event: google.maps.MapMouseEvent) => voiddrag
onDragEnd(event: google.maps.MapMouseEvent) => voiddragend
onBoundsChanged(bounds: google.maps.LatLngBoundsLiteral) => voidbounds_changed
onLoad(rectangle: google.maps.Rectangle) => voidCalled once after the rectangle is created.

Usage

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

	let bounds = $state({ north: 35.70, south: 35.66, east: 139.79, west: 139.73 });
</script>

<div style="height: 400px;">
	<APIProvider apiKey="YOUR_API_KEY">
		<GoogleMap center={{ lat: 35.68, lng: 139.76 }} zoom={13}>
			<Rectangle bind:bounds fillColor="#16a34a" fillOpacity={0.2} strokeColor="#16a34a" editable />
		</GoogleMap>
	</APIProvider>
</div>
svelte