-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimportmap.html
100 lines (86 loc) · 3.4 KB
/
importmap.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!doctype html>
<!--
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<title>Map / Marker example using importmaps</title>
<style>
body {
margin: 0;
}
#map {
width: 100vw;
height: 100vh;
}
</style>
<!-- Import maps polyfill -->
<script
async
src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"
></script>
<script type="importmap">
{
"imports": {
"@googlemaps/marker": "https://unpkg.com/@googlemaps/adv-markers-utils/dist/index.module.js",
"@googlemaps/marker/icons": "https://unpkg.com/@googlemaps/adv-markers-utils/dist/icons.module.js",
"@googlemaps/marker/places": "https://unpkg.com/@googlemaps/adv-markers-utils/dist/places.module.js"
}
}
</script>
<script>
// get API key from URL
const url = new URL(location);
const GOOGLE_MAPS_API_KEY = url.searchParams.get('apiKey');
if (!GOOGLE_MAPS_API_KEY) {
const key = prompt(
'Please provide your Google Maps API key in the URL\n' +
'(using the parameter `?apiKey=YOUR_API_KEY_HERE`) or enter it here:'
);
if (key) {
url.searchParams.set('apiKey', key);
location.replace(url.toString());
}
}
// Google Maps importLibrary loader
// see: https://developers.google.com/maps/documentation/javascript/dynamic-loading
// prettier-ignore
(g=>{var h,b,k,n="The Google Maps JavaScript API",c="google",l="importLibrary",p="__ib__",q=document,a=window;a=a[c]||(a[c]={});var d=a.maps||(a.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,m)=>{await (b=q.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."+p);b.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[p]=f;b.onerror=()=>h=m(Error(n+" could not load."));q.head.append(b)}));d[l]?console.warn(n+" only loads once. Ignoring:",g):d[l]=(f,...m)=>r.add(f)&&u().then(()=>d[l](f,...m))})
({
key: GOOGLE_MAPS_API_KEY,
v: "beta"
});
</script>
</head>
<body>
<div id="map"></div>
<script type="module">
import {Marker} from '@googlemaps/marker';
async function main() {
const {Map} = await google.maps.importLibrary('maps');
await google.maps.importLibrary('marker');
const map = new Map(document.querySelector('#map'), {
mapId: 'bf51a910020fa25a',
center: {lat: 53.555, lng: 10.001},
zoom: 12
});
const marker = new Marker({map, position: {lat: 53.555, lng: 10.001}});
}
main();
</script>
</body>
</html>