Embedding Google Maps into your HTML page allows you to integrate interactive maps directly into your web applications. Whether you want to display a location, provide directions, or show a store locator, this guide will help you embed Google Maps using simple HTML and JavaScript code.
Step 1: Obtain a Google Maps API Key
Before you can embed Google Maps, you need a Google Maps API key. Here’s how to get one:
- Sign In to Google Cloud Console
- Go to the Google Cloud Console.
- Sign in with your Google account. If you don’t have an account, create one.
- Create a New Project
- Click on the project drop-down menu at the top of the page.
- Select New Project.
- Enter a project name and click Create.
- Enable Google Maps API
- Go to APIs & Services > Library.
- Search for Maps JavaScript API and click Enable.
- Generate an API Key
- Navigate to APIs & Services > Credentials.
- Click Create Credentials and select API Key.
- Copy the generated API key.
Step 2: Set Up Your HTML Page
- Create an HTML File
- Open your text editor and create a new HTML file (e.g.,
index.html
).
- Open your text editor and create a new HTML file (e.g.,
- Add Basic HTML Structure
- Include the following basic HTML structure:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Maps Integration</title>
<style>
#map {
height: 100vh; /* Full height of the viewport */
width: 100%; /* Full width */
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
// Create a map centered at a specific location
var location = {lat: -34.397, lng: 150.644}; // Latitude and longitudevar map = new google.maps.Map(document.getElementById('map'), {
zoom: 8, // Zoom level
center: location // Center location
});// Add a marker at the specified location
</script>
var marker = new google.maps.Marker({
position: location,
map: map,
title: 'Hello World!'
});
}
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script>
</body>
</html>
- Replace
YOUR_API_KEY
with the API key you obtained earlier.
- Include the following basic HTML structure:
Step 3: Understanding the Code
- HTML Structure
- The
<style>
tag ensures the map takes up the full viewport height and width. - The
<div id="map"></div>
is where the map will be displayed.
- The
- JavaScript for Google Maps
- The
initMap
function initializes the map. It creates a map object, centers it at the specified latitude and longitude, and sets the zoom level. - A marker is added to the map at the specified location with a title.
- The
- Loading the Google Maps API
- The
<script>
tag includes the Google Maps JavaScript API and calls theinitMap
function once the API is loaded.
- The
Step 4: Test Your Map
- Save Your HTML File
- Save your changes and open the file in a web browser.
- Verify Map Display
- Ensure that the map displays correctly, centered at the specified location with the marker shown.
Conclusion
Embedding Google Maps in your HTML page is a straightforward process that enhances user experience by providing interactive maps. By following these steps, you can integrate Google Maps into your web applications and customize it to fit your needs.
This guide covered obtaining an API key, setting up your HTML page, and understanding the key components of the code. You can now explore additional features and customization options to further enhance your map integration.