Skip to content

Commit ff46439

Browse files
authored
feat: Updates add-map demo to use gmp-map. (#843)
1 parent 671f87b commit ff46439

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

samples/add-map/index.html

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<!--
33
@license
44
Copyright 2025 Google LLC. All Rights Reserved.
55
SPDX-License-Identifier: Apache-2.0
66
-->
7-
<!--[START maps_add_map]-->
7+
<!--[START maps_add_map]-->
88
<html>
99
<head>
1010
<title>Add a Map</title>
1111

1212
<link rel="stylesheet" type="text/css" href="./style.css" />
1313
<script type="module" src="./index.js"></script>
14+
<!-- prettier-ignore -->
15+
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
16+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
1417
</head>
1518
<body>
16-
<!--[START maps_add_map_heading]-->
17-
<h3>My Google Maps Demo</h3>
18-
<!--[END maps_add_map_heading]-->
1919
<!--[START maps_add_map_body]-->
20-
<div id="map"></div>
20+
<!-- The map, centered at Uluru, Australia. -->
21+
<gmp-map center="-25.344,131.031" zoom="4" map-id="DEMO_MAP_ID">
22+
<!--[START maps_add_map_controls]-->
23+
<div id="controls" slot="control-inline-start-block-start">
24+
<h3>My Google Maps Demo</h3>
25+
</div>
26+
<!--[END maps_add_map_controls]-->
27+
</gmp-map>
2128
<!--[END maps_add_map_body]-->
22-
<!-- prettier-ignore -->
23-
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
24-
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
2529
</body>
2630
</html>
27-
<!--[END maps_add_map]-->
31+
<!--[END maps_add_map]-->

samples/add-map/index.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,39 @@
55
*/
66

77
// [START maps_add_map]
8-
// Initialize and add the map.
9-
let map;
108
async function initMap(): Promise<void> {
119
// [START maps_add_map_instantiate_map]
12-
// The location of Uluru, Australia.
13-
const position = {lat: -25.344, lng: 131.031};
14-
10+
// [START maps_add_map_libraries]
1511
// 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;
12+
const [{ Map }, { AdvancedMarkerElement }] = await Promise.all([
13+
google.maps.importLibrary("maps") as Promise<google.maps.MapsLibrary>,
14+
google.maps.importLibrary("marker") as Promise<google.maps.MarkerLibrary>,
15+
]);
16+
// [END maps_add_map_libraries]
17+
// [START maps_add_map_innermap]
18+
// Get the gmp-map element.
19+
const mapElement = document.querySelector(
20+
"gmp-map"
21+
) as google.maps.MapElement;
22+
23+
// Get the inner map.
24+
const innerMap = mapElement.innerMap;
2025

21-
// The map, centered at Uluru, Australia.
22-
map = new Map(document.getElementById('map') as HTMLElement, {
23-
zoom: 4,
24-
center: position,
25-
mapId: 'DEMO_MAP_ID',
26+
// Set map options.
27+
innerMap.setOptions({
28+
mapTypeControl: false,
2629
});
30+
// [END maps_add_map_innermap]
2731
// [END maps_add_map_instantiate_map]
2832

2933
// [START maps_add_map_instantiate_marker]
30-
// The marker, positioned at Uluru.
31-
const marker = new AdvancedMarkerElement({map, position, title: 'Uluru'});
32-
// [END maps_add_map_instantiate_marker]
34+
// Add a marker positioned at the map center (Uluru).
35+
const marker = new AdvancedMarkerElement({
36+
map: innerMap,
37+
position: mapElement.center,
38+
title: "Uluru/Ayers Rock",
39+
});
40+
// [END maps_add_map_instantiate_marker]
3341
}
3442
initMap();
3543
// [END maps_add_map]

samples/add-map/style.css

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
/* [START maps_add_map] */
7-
/*
7+
/*
88
* Always set the map height explicitly to define the size of the div element
99
* that contains the map.
1010
*/
11-
#map {
12-
height: 100%;
13-
}
14-
15-
/*
11+
gmp-map {
12+
height: 100%;
13+
}
14+
15+
/*
1616
* Optional: Makes the sample page fill the window.
1717
*/
18-
html,
19-
body {
20-
height: 100%;
21-
margin: 0;
22-
padding: 0;
23-
}
24-
/* [END maps_add_map] */
25-
18+
html,
19+
body {
20+
height: 100%;
21+
margin: 0;
22+
padding: 0;
23+
}
24+
/* [END maps_add_map] */
25+
26+
#controls h3 {
27+
font-size: 1.5em;
28+
background-color: white;
29+
margin: 8px;
30+
padding: 2px;
31+
}

0 commit comments

Comments
 (0)