Created
February 13, 2023 01:13
-
-
Save siniradam/fdd110c4c5c31fc70ba76d01af135687 to your computer and use it in GitHub Desktop.
Google maps component for SvelteKit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const dispatch = createEventDispatcher(); | |
import { onMount } from 'svelte'; | |
import { createEventDispatcher } from 'svelte'; | |
//import mapStyles from './map-styles'; // optional | |
const key = ''; | |
export let globally = false; | |
export let map; | |
// @ts-ignore | |
let container; | |
let zoom = 8; | |
let center = { lat: 37.5742776, lng: 43.7260158 }; | |
let src = ''; | |
onMount(() => { | |
Object.assign(window, { | |
mapLoaded: () => { | |
// @ts-ignore | |
map = new google.maps.Map(container, { | |
zoom, | |
center | |
// styles: mapStyles | |
}); | |
dispatch('load', true); | |
if (globally) { | |
Object.assign(window, { map }); | |
} | |
} | |
}); | |
//Import Google Maps API. | |
src = `https://maps.googleapis.com/maps/api/js?key=${key}&callback=mapLoaded`; | |
}); | |
</script> | |
<div class="w-full h-full" bind:this={container} /> | |
<svelte:head> | |
{#if src} | |
<script {src}></script> | |
{/if} | |
</svelte:head> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment