Skip to content

Commit 008cdf7

Browse files
committed
Google maps dynamic import
1 parent 12fe1b0 commit 008cdf7

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

demo/bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/bundle.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/demo.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { asyncBufferFromUrl } from 'hyparquet'
22
import { toGeoJson } from '../src/index.js'
33

4-
// @ts-expect-error need global function for google maps
5-
window.initMap = async function loadGeoParquet() {
4+
async function initMap() {
5+
// @ts-expect-error MapsLibrary
6+
const { Map } = await google.maps.importLibrary('maps')
67
const div = /** @type {HTMLElement} */(document.getElementById('map'))
78
// Create a new map
8-
const map = new google.maps.Map(div, {
9+
const map = new Map(div, {
910
center: { lat: 39, lng: -98 },
1011
zoom: 4,
1112
})
@@ -26,3 +27,4 @@ window.initMap = async function loadGeoParquet() {
2627
console.error('Error loading or parsing GeoParquet file:', error)
2728
}
2829
}
30+
initMap()

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
<!-- Load demo js -->
1111
<script type="module" src="demo/bundle.min.js"></script>
1212
<!-- Load Google Maps JavaScript API -->
13-
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBQSLAwuvTBUrsDpwe17SFdIHo4PJXE1-Q&loading=async&callback=initMap"></script>
13+
<script>
14+
(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))})({
15+
key: "AIzaSyBQSLAwuvTBUrsDpwe17SFdIHo4PJXE1-Q",
16+
})
17+
</script>
1418
</head>
1519
<body>
1620
<div id="map"></div>

0 commit comments

Comments
 (0)