The following example it's a simple example to use google maps api with Svelte
The project requires to have an Google Cloud Project, and activate the Maps Javascript Api
To create a Google Cloud Project and enable the Maps JavaScript API, follow these steps:
Once you have completed these steps, you will have created a Google Cloud Project and enabled the Maps JavaScript API for your project.
To get the API key for your project, follow these steps:
Go the root folder of the project and execute
npm install
The main package to use is @googlemaps/js-api-loader
Open src/App.svelte
and to the line #8
const loader = new Loader({
apiKey: '<<Your api key>>',
version: 'weekly'
});
And set your API key in the key apiKey
If you want to set the coordinate, set the values on the key center
onMount(async () => {
const { Map } = await loader.importLibrary('maps');
map = new Map(mapContainer, {
center: { lat: -34.397, lng: 150.644 },
zoom: 8
});
});
npm run dev