
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to Add Google Maps to a Website?
In this article, we will be exploring Google Maps and how to add or embed google maps into our HTML template/website. Once the Google Maps are embedded into the website we can pin a location in it to display the user the current position of the store or a company.
Steps
Below are the steps that need to be followed to embed Google Maps into your HTML pages −
We need to generate an API key to be able to use Google maps privately.
Creating the HTML container to place Google Maps.
Adding the external script provided by Google inside the HTML document.
Write the JavaScript code to initialize the map inside the container.
We will be exploring the above points in detail below −
Generating the API Key
We can fetch the location data from Google Maps, but before proceeding with this we would require the Google Maps key to be generated. This key will authorize the collection of the data from google maps. You need to follow the below simple steps to generate this key −
Click & visit the following link to open the Google Cloud Console.
Create or select an already created Project.
Click continue for enabling the API and enable the Maps JavaScript API.
Once enabled now we need to create credentials for the same.
Now navigate to the credentials section of the menu located in the left sidebar.
Click on the “Create Credentials” button below the navigation bar and choose “API Key” to start generating the credentials.
Once the credentials are created you will get an API key.
You can use this key to embed Map in your website.
Creating the HTML container to place Google Maps.
Now we will be creating an HTML container for placing the google maps. We will be using simple div elements for this example. You can use anything you like. You need to follow the following steps for the same −
Creating a new HTML page.
Introducing the empty div tag inside the body section. Also, assign a unique ID to this div so that it could be used later for styling.
Now create a style tag and describe the size of the div inside this tag.
Adding the external script provided by Google inside the HTML document
You would need to add the below async script inside your HTML document to execute it immediately. This script should be after the DOM elements used in the callback. Inside the URL, you would be required to put in your API key that is being generated earlier by replacing the <YOUR_API_KEY> tag.
<script src=“https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>&callback=initMap&libraries=&v=weekly” async> </script>
Writing the JavaScript code to initialize the map inside the container
After the HTML is setup we would need to initialize the JavaScript to bring the map inside the container and also give controls to the map for the user. It would support controls like zoom in, zoom out, scroll, etc.
Initialize the initMap() function. This name cannot be changes as this is the inbuilt functions used by the Maps API.
Initialize the latitude and longitude inside this object to setup the location you want to display.
Create a new google.maps.Map object that will take container elements as input. This object will be responsible for storing the centre location and zoom of the map.
Example
# map.html
<!DOCTYPE html> <html> <head> <style type="text/css"> /* Set the size of the div element that contains the map */ #map { height: 400px; width: 400px; } h2 { color: #308d46; } </style> </head> <body> <h1 style="color: green;">Welcome to Tutorials Point</h1> <h2 style="color: grey;"> Add Google Map to your Webpage </h2> <!--The div element for the map --> <div id="map"></div> <!--Adding script by google --> <script src="https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>&callback=initMap&libraries=&v=weekly"async></script> <script> // Initialize and add the map function initMap() { // The location of Geeksforgeeks office const tutorialsPoint_office = { lat: 17.43827944602866, lng: 78.39530424154626 }; // Create the map, centered at tutorialsPoint_office const map = new google.maps.Map( document.getElementById("map"), { // Set the zoom of the map zoom: 17.56, center: tutorialsPoint_office, }); } </script> </body> </html>
Output
- Related Articles
- Google Maps Alternatives
- How to use HTML5 GeoLocation API with Google Maps?
- How to launch Google Maps Directions via an intent on Android?
- How to add a YouTube Video to your Website?
- Why Google embedded Map Makers inside Google Maps?
- How to add authentication to Django Website?
- 10 Key Steps To Ranking Higher In Google Maps
- How to add a captcha in a Django website?
- How to add security to your Django website?
- How to add authorization to your Django website?
- How to add YouTube video to Google slides?
- How to add some non-standard font to a website in CSS?
- How to add Google Charts to your web page?
- How to add Google Search Functionality in Kotlin?
- Call Whole World Through New Android’s Google Maps
