When building websites, including a map is often necessary to display important locations, such as office addresses, at the footer or on dedicated pages. Adding a map enhances user experience, making it easier for users to locate your business or other important points of interest.
Prerequisites
- Basic knowledge of HTML and CSS is required to follow along with the examples.
Methods to Add a Map in HTML
Now, let's explore two popular approaches for adding maps to a webpage:
Now we will learn one by one how we can add maps in HTML.
1. Using <iframe> to Embed Google Maps
The easiest and most commonly used method to add a map in HTML is by embedding a Google Map using the <iframe> tag. Google Maps provides an option to generate an embed code that you can copy and paste directly into your HTML document.
Steps to Embed Google Maps:
- Go to Google Maps.
- Search for the location you want to display.
- Click the Share option and choose Embed a Map.
- Copy the provided <iframe> code and paste it into your HTML where you want the map to appear.
Syntax:
<iframe src="https://www.google.com/maps/embed?pb=YourMapURL"
width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy">
</iframe>
Example: Below given example demonstrates how to embed a goggle map into a webpage using an <iframe> element. It shows a webpage with centered heading and a google map showing specific location.
HTML
<!-- Using iframe -->
<!DOCTYPE html>
<html>
<head>
<title>Embed Google Map</title>
</head>
<body style="display: grid;
justify-content: center">
<h2 style="text-align: center">
Add map in html using iframe</h2>
<iframe src="https://www.google.com/maps/embed?pb
=!1m18!1m12!1m3!1d4291.745453141256!2d
77.39914006293347!3d28.504393188935435!
2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1
!3m3!1m2!1s0x390ce626851f7009%3A0x62118
5133cfd1ad1!2sGeeksforGeeks%20%7C%20
Coding%20Classes%20%7C%20Noida!5e0!3m2!1
sen!2sin!4v1725395799894!5m2!1sen!2sin" width="600" height="450" style="border: 0" allowfullscreen=""
loading="lazy" referrerpolicy="no-referrer-when-downgrade">
</iframe>
</body>
</html>
Output:
Add map in HTML using iframe2. Using Leaflet.js to Add Interactive Maps
Leaflet is a popular open source library to add interactive maps in our website. It is a Javascript library for mobile friendly lighweight and interactive maps. It is designed to be easy to use and mobile-friendly.It is customizable, as you can customize it with variety of additional plugins.
Steps to Use Leaflet.js:
- Include the Leaflet.js library: Link the CSS and JavaScript files in your HTML document.
- Create a map container: Add a <div> element where the map will be displayed.
- Initialize the map: Use Leaflet.js to create the map and set the center coordinates and zoom level.
Syntax:
let map = L.map('map').setView([Latitude, Longitude], ZoomLevel);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker([Latitude, Longitude]).addTo(map)
.bindPopup('Popup Text Here.')
.openPopup();
Example: Below is the example for creating interactive map in our website using leaflet.js , it is a open-source javascript library. The map centers on India Gate, New Delhi and displays it with marker.
HTML
<!-- Using leaflet Map -->
<!DOCTYPE html>
<html>
<head>
<title>Leaflet Map Example</title>
<link rel="stylesheet" href="
https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZy
oHS5obTRR9BMY=" crossorigin="" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9V
M+kNiyxNV1lvTlZBo=" crossorigin=""></script>
</head>
<body>
<h2 style="text-align: center;">
India Gate Location</h2>
<div id="mapid" style="height: 75vh; width: 50vw;
margin-left: 25%;"></div>
<script>
var map = L.map("mapid").setView
([28.612894, 77.229446], 13); L.tileLayer
("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors",
}).addTo(map);
L.marker([28.612894, 77.229446])
.addTo(map)
.bindPopup("India Gate, New Delhi")
.openPopup();
</script>
</body>
</html>
Output:
Add map in html using leaflet
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read