KmlLayer

A component to display data from a KML (Keyhole Markup Language) file on the map.

Must be used within a <GoogleMap> component.

Note: KML layers load KML files from a publicly accessible URL. Local files are not supported.

<script lang="ts">
  import { APIProvider, GoogleMap, KmlLayer } from 'svelte-google-maps-api';
  import { GOOGLE_MAPS_API_KEY } from '$env/static/private';

  // Example KML URL (replace with your own publicly accessible KML file)
  const kmlUrl = 'https://developers.google.com/maps/documentation/javascript/examples/kml/westcampus.kml';
</script>

<APIProvider apiKey={GOOGLE_MAPS_API_KEY}>
  <GoogleMap initialCenter={{ lat: 45.518, lng: -122.672 }} initialZoom={11}>
    <KmlLayer url={kmlUrl} suppressInfoWindows={true} />
  </GoogleMap>
</APIProvider>
svelte

Props

PropTypeDefaultDescription
urlstring | undefinedundefinedThe public URL of the KML file to load. Required. Changing the URL will cause the layer to re-initialize.
optionsgoogle.maps.KmlLayerOptions | undefinedundefinedInitialization options for the KML layer. Takes precedence over individual props.
clickableboolean | undefinedundefinedWhether features in the layer are clickable.
preserveViewportboolean | undefinedundefinedWhether to prevent the map from adjusting its viewport to fit the KML file's content if it contains viewport information. Defaults to false.
screenOverlaysboolean | undefinedundefinedWhether to display screen overlays from the KML file.
suppressInfoWindowsboolean | undefinedundefinedWhether to suppress the display of info windows when features are clicked.
zIndexnumber | undefinedundefinedThe z-index of the layer.

Events

EventTypeDescription
onClick((e: google.maps.KmlMouseEvent) => void) | undefinedFired when a feature in the layer is clicked. The event object contains information about the clicked feature.
onDefaultViewportChanged(() => void) | undefinedFired when the layer's default viewport has loaded.
onStatusChanged(() => void) | undefinedFired when the layer's loading status changes.
onLoad((layer: google.maps.KmlLayer) => void) | undefinedFired when the layer has successfully loaded. Receives the instance.
onUnmount((layer: google.maps.KmlLayer) => void) | undefinedFired when the component is destroyed. Receives the instance.

Binding

This component does not support the bind: directive.