Adding multiple markers on a Google Map using Webflow and the Google Maps API involves a few steps. Here’s a structured guide to help you through the process.
1. Set Up Google Maps API
- Go to Google Cloud Console and create a new project or select an existing project.
- Navigate to APIs & Services and click on Enable APIs and Services.
- Search for and enable the Maps JavaScript API.
- If needed, configure your billing details to ensure proper API access.
- Generate an API key under Credentials and ensure it is restricted to your website’s domain for security.
2. Prepare Your Webflow Project
- Create a new page or identify a page where you want the map displayed.
- Add an HTML Embed element where you want the map to appear.
- In the Embed element, prepare to insert the Google Maps script using your API key.
3. Script to Add Multiple Markers
- In the HTML Embed, include a script tag that will call the Google Maps API.
- Within the script, set up a function to:
- Initialize the map with specified center locations and zoom level.
- Add multiple marker positions using an array of coordinates.
- Example of inline reference code for markers:
```js
var map;
function initMap() {
var centerLocation = { lat: -34.397, lng: 150.644 };
map = new google.maps.Map(document.getElementById('map'), {
center: centerLocation,
zoom: 8
});
var markers = [
{ position: { lat: -34.397, lng: 150.644 }, title: 'Location 1' },
{ position: { lat: -35.297, lng: 149.644 }, title: 'Location 2' },
];
markers.forEach(function(markerInfo) {
new google.maps.Marker({
position: markerInfo.position,
map: map,
title: markerInfo.title
});
});
}
```
4. Test Your Map
- Publish your changes in Webflow and view the page to ensure that the Google Map appears.
- Verify that all desired markers are correctly placed on the map.
- Troubleshoot any errors by checking the browser console and ensuring the API key usage is well within limits.
Summary
To add multiple markers on a Google Map in Webflow, you need to enable the Maps JavaScript API, get an API key, and then integrate a custom script in an HTML Embed within your Webflow project. Initiate the map and loop through your markers to place them properly. Always ensure your API key is secured and your Google Cloud project is adequately set up.