|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_circle_simple] |
| 8 | +const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; |
| 9 | +let innerMap; |
| 10 | + |
| 11 | +async function initMap() { |
| 12 | + // Import the needed libraries. |
| 13 | + // Request needed libraries. |
| 14 | + (await google.maps.importLibrary('maps')) as google.maps.MapsLibrary; |
| 15 | + (await google.maps.importLibrary('marker')) as google.maps.MarkerLibrary; |
| 16 | + // Get the gmp-map element. |
| 17 | + const mapElement = document.querySelector( |
| 18 | + 'gmp-map' |
| 19 | + ) as google.maps.MapElement; |
| 20 | + |
| 21 | + const initialCenter = { lat: 34.98956821576194, lng: 135.74239981260283 }; // Hotel Emion, Kyoto, Japan |
| 22 | + |
| 23 | + // Get the inner map. |
| 24 | + const innerMap = mapElement.innerMap; |
| 25 | + |
| 26 | + const buttons = document.querySelectorAll('input[name="radius"]'); |
| 27 | + |
| 28 | + const walkingCircle = new google.maps.Circle({ |
| 29 | + strokeColor: '#ffdd00ff', |
| 30 | + strokeOpacity: 0.8, |
| 31 | + strokeWeight: 2, |
| 32 | + fillColor: '#ffdd00ff', |
| 33 | + fillOpacity: 0.35, |
| 34 | + map: innerMap, |
| 35 | + center: initialCenter, |
| 36 | + radius: 400, |
| 37 | + draggable: true, |
| 38 | + editable: false, |
| 39 | + }); |
| 40 | + |
| 41 | + // Define a "Crosshair" vector icon |
| 42 | + const parser = new DOMParser(); |
| 43 | + const svgString = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="-6 -6 12 12"><path d="M -6,0 L 6,0 M 0,-6 L 0,6" stroke="black" stroke-width="1"/></svg>`; |
| 44 | + |
| 45 | + const pinSvg = parser.parseFromString( |
| 46 | + svgString, |
| 47 | + 'image/svg+xml' |
| 48 | + ).documentElement; |
| 49 | + |
| 50 | + const centerMarker = new google.maps.marker.AdvancedMarkerElement({ |
| 51 | + position: initialCenter, |
| 52 | + title: 'A marker using a custom SVG image.', |
| 53 | + //@ts-ignore |
| 54 | + anchorLeft: '-50%', |
| 55 | + anchorTop: '-50%', |
| 56 | + }); |
| 57 | + centerMarker.append(pinSvg); |
| 58 | + mapElement.append(centerMarker); |
| 59 | + |
| 60 | + // Wait for the map to finish drawing its tiles. |
| 61 | + google.maps.event.addListenerOnce(innerMap, 'tilesloaded', function () { |
| 62 | + // Get the controls div |
| 63 | + const controls = document.getElementById('control-panel'); |
| 64 | + |
| 65 | + // Display controls once map is loaded. |
| 66 | + if (controls) { |
| 67 | + controls.style.display = 'block'; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + // Add event listener to update the radius based on user selection. |
| 72 | + buttons.forEach((button) => { |
| 73 | + button.addEventListener('change', (event) => { |
| 74 | + const target = event.target as HTMLInputElement; |
| 75 | + walkingCircle.setRadius(Number(target.value)); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + // Handle user click, reset the map center and position the circle. |
| 80 | + innerMap.addListener('click', (mapsMouseEvent) => { |
| 81 | + const newCenter = mapsMouseEvent.latLng; |
| 82 | + walkingCircle.setCenter(newCenter); |
| 83 | + centerMarker.position = newCenter; |
| 84 | + innerMap.panTo(newCenter); |
| 85 | + }); |
| 86 | + |
| 87 | + // Handle user dragging the circle, update the center marker position. |
| 88 | + walkingCircle.addListener('center_changed', () => { |
| 89 | + centerMarker.position = walkingCircle.getCenter(); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +initMap(); |
| 94 | +// [END maps_circle_simple] |
0 commit comments