Polyline

Storybook

Open the Polyline story

Polyline draws a google.maps.Polyline. With editable or draggable, the user's edits flow back through bind:path.

Props

PropTypeDefaultDescription
pathgoogle.maps.LatLngLiteral[]Vertices of the line. Bindable: use bind:path to track edits.
strokeColorstringStroke color.
strokeOpacitynumberStroke opacity between 0 and 1.
strokeWeightnumberStroke width in pixels.
geodesicbooleanDraw great-circle segments.
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.
iconsgoogle.maps.IconSequence[]Symbols along the line.
optionsgoogle.maps.PolylineOptionsOther polyline options. Individual props override these.
polylinegoogle.maps.Polyline | nullnullThe instance. Bindable: read it with bind:polyline.

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
onPathChanged(path: google.maps.LatLngLiteral[]) => voidCalled after the user edits or drags the line.
onLoad(polyline: google.maps.Polyline) => voidCalled once after the polyline is created.

Usage

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

	let path = $state([
		{ lat: 35.6812, lng: 139.7671 },
		{ lat: 35.6586, lng: 139.7454 },
		{ lat: 35.71, lng: 139.8107 }
	]);
</script>

<div style="height: 400px;">
	<APIProvider apiKey="YOUR_API_KEY">
		<GoogleMap center={{ lat: 35.68, lng: 139.77 }} zoom={12}>
			<Polyline bind:path strokeColor="#0f766e" strokeWeight={4} geodesic editable />
		</GoogleMap>
	</APIProvider>
</div>

<p>{path.length} vertices</p>
svelte

Notes

  • Style props are applied with setOptions() whenever they change; unset props keep the API defaults.
  • Assigning a new path array redraws the line. Editing or dragging writes a new array back to path.