Data

Storybook

Open the Data story

Data adds a google.maps.Data layer for GeoJSON features. Load features from an object or a URL, and style them declaratively.

Props

PropTypeDefaultDescription
geoJsonobjectGeoJSON object to add. Replacing it removes the previous features.
urlstringURL of a GeoJSON file to load. Changing it removes the previously loaded features.
idPropertyNamestringFeature property used as the feature ID.
stylegoogle.maps.Data.StylingFunction | google.maps.Data.StyleOptionsFeature style, or a function returning a style per feature.
optionsgoogle.maps.Data.DataOptions (without map and style)Other options (controls, drawing mode, ...). Creation-only.
datagoogle.maps.Data | nullnullThe instance. Bindable: read it with bind:data.

Event Props

PropTypeDescription
onClick(event: google.maps.Data.MouseEvent) => voidclick
onDblClick(event: google.maps.Data.MouseEvent) => voiddblclick
onContextMenu(event: google.maps.Data.MouseEvent) => voidcontextmenu
onMouseDown(event: google.maps.Data.MouseEvent) => voidmousedown
onMouseUp(event: google.maps.Data.MouseEvent) => voidmouseup
onMouseOver(event: google.maps.Data.MouseEvent) => voidmouseover
onMouseOut(event: google.maps.Data.MouseEvent) => voidmouseout
onAddFeature(event: google.maps.Data.AddFeatureEvent) => voidaddfeature
onRemoveFeature(event: google.maps.Data.RemoveFeatureEvent) => voidremovefeature
onSetGeometry(event: google.maps.Data.SetGeometryEvent) => voidsetgeometry
onSetProperty(event: google.maps.Data.SetPropertyEvent) => voidsetproperty
onRemoveProperty(event: google.maps.Data.RemovePropertyEvent) => voidremoveproperty
onLoad(data: google.maps.Data) => voidCalled once after the layer is created.

Usage

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

	const geoJson = {
		type: 'FeatureCollection',
		features: [
			{
				type: 'Feature',
				properties: { name: 'Imperial Palace' },
				geometry: {
					type: 'Polygon',
					coordinates: [
						[
							[139.745, 35.68],
							[139.76, 35.68],
							[139.76, 35.69],
							[139.745, 35.69],
							[139.745, 35.68]
						]
					]
				}
			}
		]
	};

	let selected = $state('');
</script>

<div style="height: 400px;">
	<APIProvider apiKey="YOUR_API_KEY">
		<GoogleMap center={{ lat: 35.685, lng: 139.752 }} zoom={14}>
			<Data
				{geoJson}
				style={{ fillColor: '#0f766e', fillOpacity: 0.3, strokeWeight: 2 }}
				onClick={(event) => (selected = String(event.feature.getProperty('name')))}
			/>
		</GoogleMap>
	</APIProvider>
</div>

<p>{selected}</p>
svelte

Notes

  • geoJson and url can be used together; each manages its own features.
  • Features added with bind:data (for example data.add(...)) are not removed automatically when geoJson or url changes.