Circle

The Circle component draws a circle on the map with a specified center and radius.

Props

PropTypeDefaultDescription
centergoogle.maps.LatLng | google.maps.LatLngLiteralCenter coordinates of the circle
optionsgoogle.maps.CircleOptionsCircle configuration options
onLoad(circle: google.maps.Circle) => voidCallback when circle is loaded
onUnmount(circle: google.maps.Circle) => voidCallback before circle is destroyed

Event Handler Props

PropTypeDescription
onClick(e: google.maps.MapMouseEvent) => voidClick event handler
onDblClick(e: google.maps.MapMouseEvent) => voidDouble click event handler
onDrag(e: google.maps.MapMouseEvent) => voidDrag event handler
onDragEnd(e: google.maps.MapMouseEvent) => voidDrag end event handler
onDragStart(e: google.maps.MapMouseEvent) => voidDrag start event handler
onMouseDown(e: google.maps.MapMouseEvent) => voidMouse down event handler
onMouseMove(e: google.maps.MapMouseEvent) => voidMouse move event handler
onMouseOut(e: google.maps.MapMouseEvent) => voidMouse out event handler
onMouseOver(e: google.maps.MapMouseEvent) => voidMouse over event handler
onMouseUp(e: google.maps.MapMouseEvent) => voidMouse up event handler
onRightClick(e: google.maps.MapMouseEvent) => voidRight click event handler
onCenterChanged() => voidCenter changed event handler
onRadiusChanged() => voidRadius changed event handler

Usage

<script>
  import { APIProvider, GoogleMap, Circle } from 'svelte-google-maps-api';
  
  const apiKey = 'YOUR_API_KEY';
  const mapOptions = {
    center: { lat: 35.6812362, lng: 139.7649361 },
    zoom: 12
  };
  
  // Circle center and options
  const center = { lat: 35.6812362, lng: 139.7649361 };
  const circleOptions = {
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    radius: 1000 // in meters
  };
  
  function handleCircleClick() {
    alert('Circle clicked!');
  }
</script>

<div style="height: 400px; width: 100%;">
  <APIProvider {apiKey}>
    <GoogleMap id="map" options={mapOptions} mapContainerStyle="width: 100%; height: 100%;">
      <Circle 
        center={center} 
        options={circleOptions}
        onClick={handleCircleClick}
      />
    </GoogleMap>
  </APIProvider>
</div>
svelte

Editable Circle

You can make the circle editable by setting the editable option to true:

<script>
  import { APIProvider, GoogleMap, Circle } from 'svelte-google-maps-api';
  
  const apiKey = 'YOUR_API_KEY';
  const mapOptions = {
    center: { lat: 35.6812362, lng: 139.7649361 },
    zoom: 12
  };
  
  // Circle center and options
  let center = { lat: 35.6812362, lng: 139.7649361 };
  let radius = 1000; // in meters
  
  const circleOptions = {
    strokeColor: '#0000FF',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#0000FF',
    fillOpacity: 0.35,
    radius,
    editable: true
  };
  
  function handleCircleChanges(circle) {
    // Get the updated center and radius
    const newCenter = {
      lat: circle.getCenter().lat(),
      lng: circle.getCenter().lng()
    };
    const newRadius = circle.getRadius();
    
    center = newCenter;
    radius = newRadius;
    
    console.log('Circle updated:', { center, radius });
  }
</script>

<div>
  <p>Circle center: {center.lat.toFixed(6)}, {center.lng.toFixed(6)}</p>
  <p>Circle radius: {radius.toFixed(2)} meters</p>
  
  <div style="height: 400px; width: 100%;">
    <APIProvider {apiKey}>
      <GoogleMap id="map" options={mapOptions} mapContainerStyle="width: 100%; height: 100%;">
        <Circle 
          center={center} 
          options={circleOptions}
          onLoad={(circle) => {
            // Add listeners for center and radius changes
            google.maps.event.addListener(circle, 'center_changed', () => handleCircleChanges(circle));
            google.maps.event.addListener(circle, 'radius_changed', () => handleCircleChanges(circle));
          }}
        />
      </GoogleMap>
    </APIProvider>
  </div>
</div>
svelte

Context

The Circle component consumes the context set by GoogleMap to access the map instance:

const { getMap } = getContext('map') ?? {};
let map = getMap();
javascript

Notes

  • The Circle component must be a child of the GoogleMap component.
  • It creates a Google Maps circle and attaches it to the map.
  • The radius is specified in meters.
  • Event listeners are automatically cleaned up when the component is destroyed.
  • The circle is automatically removed from the map when the component is unmounted.