|
3 | 3 | * Copyright 2025 Google LLC. All Rights Reserved. |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
| 6 | +/* |
| 7 | + * This is a special sample! Having a dedicated test sample makes it easier to |
| 8 | + * test changes in a world where sometimes changes must be merged in order to |
| 9 | + * test them. This way we can avoid making changes to published content. |
| 10 | + */ |
| 11 | + |
6 | 12 | // TEST COMMENT 007 |
7 | 13 | // [START maps_test_example] |
8 | | -// Initialize and add the map. |
9 | | -let map; |
| 14 | +// Declare the gmp-map element. |
| 15 | +const mapElement = document.querySelector("gmp-map") as google.maps.MapElement; |
| 16 | +let innerMap; |
| 17 | +const advancedMarkerElement = document.querySelector( |
| 18 | + "gmp-advanced-marker" |
| 19 | +) as google.maps.marker.AdvancedMarkerElement; |
| 20 | +let center; |
| 21 | + |
10 | 22 | async function initMap(): Promise<void> { |
11 | 23 | // [START maps_test_example_instantiate_map] |
12 | | - // The location of Uluru. |
13 | | - const position = {lat: -25.344, lng: 131.031}; |
14 | | - |
15 | 24 | // Request the needed libraries. |
16 | | - const {Map} = |
17 | | - await google.maps.importLibrary('maps') as google.maps.MapsLibrary; |
18 | | - const {AdvancedMarkerElement} = |
19 | | - await google.maps.importLibrary('marker') as google.maps.MarkerLibrary; |
| 25 | + const { Map } = (await google.maps.importLibrary( |
| 26 | + "maps" |
| 27 | + )) as google.maps.MapsLibrary; |
| 28 | + const { AdvancedMarkerElement } = (await google.maps.importLibrary( |
| 29 | + "marker" |
| 30 | + )) as google.maps.MarkerLibrary; |
20 | 31 |
|
21 | | - // The map, centered at Uluru. |
22 | | - map = new Map(document.getElementById('map') as HTMLElement, { |
23 | | - zoom: 4, |
24 | | - center: position, |
25 | | - mapId: 'DEMO_MAP_ID', |
| 32 | + // Get the inner map from the gmp-map element. |
| 33 | + innerMap = mapElement.innerMap; |
| 34 | + innerMap.setOptions({ |
| 35 | + mapTypeControl: false, |
26 | 36 | }); |
27 | 37 | // [END maps_test_example_instantiate_map] |
28 | 38 |
|
| 39 | + // Get the lat/lng from the inner map. |
| 40 | + center = innerMap.getCenter(); |
| 41 | + |
29 | 42 | // [START maps_test_example_instantiate_marker] |
30 | | - // The marker, positioned at Uluru. |
31 | | - const marker = new AdvancedMarkerElement({map, position, title: 'Uluru'}); |
32 | | - // [END maps_test_example_instantiate_marker] |
| 43 | + // Add a marker, positioned at Uluru. |
| 44 | + const marker = new AdvancedMarkerElement({ map: innerMap, position: center, title: "Uluru" }); |
| 45 | + // [END maps_test_example_instantiate_marker] |
33 | 46 | } |
34 | 47 | initMap(); |
35 | 48 | // [END maps_test_example] |
0 commit comments