• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
Home Guides Programming JavaScript

Add an OpenStreetMap to Your Website

Jonathan Moore by Jonathan Moore
5 years ago
Reading Time: 5 mins read
A A
Add an OpenStreetMap to Your Website
FacebookTwitter

In my previous guide, I showed you how to add a Google Map to your website by using the Google Maps API. If you don’t want to use Google as your mapping engine then this guide is for you. In this guide, I will show you how to use OpenStreetMap as your mapping engine, which will not require you to set up an API Key. We will also be using leaflet.js to make things a whole lot easier.

Let’s Get Started

I will be using the same example as I did in the “Add a Google Map to Your Website” guide that I posted previously. A lot of this guide is going to look very similar except for the necessary changes that are needed.

Create an HTML Page

This is the basic HTML for setting up our website to include the Google Map. This template will display a map that has a height of 480px and a width of 640px. The #map ID within the style tag allows you to further adjust the styling of the map’s DIV container.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <style>
            body {
                margin: 0;
            }
            #map {
                width: 640px;
                height: 480px;
                background-color: grey;
            }
        </style>
        <title>My First OpenStreetMap</title>
   </head>

   <body>

        <div id="map"></div>

   </body>
</html>

Add the Leaflet Library

The Leaflet library is the leading open-source JavaScript library for mobile-friendly interactive maps. Weighing just about 39 KB of JS, it has all the mapping features most developers ever need. It is designed with simplicity, performance, and usability in mind. It works efficiently across all major desktop and mobile platforms, can be extended with lots of plugins, has a beautiful, easy-to-use, and well-documented API.

Now that we have our basic HTML setup, we will need to modify it to add a call to the leaflet.css file in our header.

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />

We will also need to add a call to the leaflet.js file to the body of our HTML structure underneath our #map DIV tag.

<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>

Below is what our new HTML structure should look like.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
        <style>
            body {
                margin: 0;
            }
            #map {
                width: 640px;
                height: 480px;
                background-color: grey;
            }
        </style>
        <title>My First OpenStreetMap</title>
   </head>

   <body>

        <div id="map"></div>

        <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>

   </body>
</html>

Adding the Callback

The only thing left before we can display the map is to initiate the callback. This is one of the most important pieces of our project. It not only displays the map, but also allows us to define our starting coordinates, markers, and additional options.

var map;

function initMap() {
    map = L.map( 'map', {
        center: [ 39.807305, -77.224207 ],
        zoom: 13
    });

    L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo( map );
}

The above callback function will display a basic hybrid map with no markers on your page. The center option will center the map to the latitude and longitude coordinates. You can adjust how far or how close you want to view the map with the zoom option. The smaller the zoom number, the further out the zoom level is, and the larger the zoom number, the closer the zoom level is.

We are also going to modify the body tag and use it to initialize the map once the page has loaded.

<body onload="initMap()">

How good is a map without markers? A map without markers isn’t very useful. We use markers to show places of interest on a map. In the following function, we are going to be defining several points of interest and generate a marker for each interest on the map.

function create_markers() {
    var markers = [
        ["Delaware State Monument", "39.8161", "-77.23247"],
        ["Indiana State Monument", "39.814138", "-77.216287"],
        ["New York State Monument", "39.8208", "-77.2306"],
        ["New York Auxiliary State Monument", "39.8043", "-77.23445"],
        ["Pennsylvania State Memorial", "39.807588", "-77.235153"],
        ["United States Regulars Monument", "39.811217", "-77.235683"],
        ["United States Signal Corps Marker", "39.79254", "-77.236503"],
        ["Vermont State Monument", "39.80945", "-77.23636"],
    ];

    for ( var i = 0; i < markers.length; i++ ) {
        marker = new L.marker( [ markers[i][1], markers[i][2] ] )
            .bindPopup( markers[i][0] )
            .addTo( map );
    }
}

The above function will iterate through a list called markers and create a marker on the map for each item in the list. The first element in each sub-list is the content that will be displayed in an info window when the marker is clicked. The second and third elements in each sub-list are the latitude and longitude for the location.

In order to use the create_markers function, we will need to call it from the initMap function. This will allow the base map to load and then the markers.

function initMap() {
    map = L.map( 'map', {
        center: [ 39.807305, -77.224207 ],
        zoom: 13
    });

    L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo( map );

    create_markers();
}

Our Final Project

We have gone through each piece of code and it’s now time to put it all together.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
        <style>
            body {
                margin: 0;
            }
            #map {
                width: 640px;
                height: 480px;
                background-color: grey;
            }
        </style>
        <title>My First OpenStreetMap</title>
   </head>

   <body onload="initMap()">

        <div id="map"></div>

        <script>
            var map;

            function create_markers() {
                var markers = [
                    ["Delaware State Monument", "39.8161", "-77.23247"],
                    ["Indiana State Monument", "39.814138", "-77.216287"],
                    ["New York State Monument", "39.8208", "-77.2306"],
                    ["New York Auxiliary State Monument", "39.8043", "-77.23445"],
                    ["Pennsylvania State Memorial", "39.807588", "-77.235153"],
                    ["United States Regulars Monument", "39.811217", "-77.235683"],
                    ["United States Signal Corps Marker", "39.79254", "-77.236503"],
                    ["Vermont State Monument", "39.80945", "-77.23636"],
                ];

                for ( var i = 0; i < markers.length; i++ ) {
                    marker = new L.marker( [ markers[i][1], markers[i][2] ] )
                        .bindPopup( markers[i][0] )
                        .addTo( map );
                }
            }

            function initMap() {
                map = L.map( 'map', {
                    center: [ 39.807305, -77.224207 ],
                    zoom: 13
                });

                L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                    attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
                }).addTo( map );

                create_markers();
            }
        </script>

        <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>

   </body>
</html>

We have completed the guide for adding an OpenStreetMap to a website. If you have done everything correctly, your page should display a map similar to the one below.



Tags: JavaScriptLeafletMappingOpenStreetMap
ShareTweetSharePinShareShareScan
ADVERTISEMENT
Jonathan Moore

Jonathan Moore

I am a Software Architect and Senior Software Engineer with 30+ years of experience building applications for Linux and Windows systems. I focus on system architecture, custom web platforms, server infrastructure, and security-focused tools, with an emphasis on performance and reliability. Over the years, I have built everything from WordPress plugins and automation systems to full platforms, ad serving systems, monitoring tools, and API-driven applications. I prefer working close to the system, solving real problems, and building tools that are meant to be used.

Related Articles

Calculating the Date of Easter Using the Computus Algorithm

Calculating the Date of Easter Using the Computus Algorithm

Easter is one of the oldest and most significant festivals in the Christian calendar, celebrating the resurrection of Jesus Christ....

Automating JavaScript Minification with PHP

Automating JavaScript Minification with PHP

Automating JavaScript minification with PHP can significantly streamline a developer's workflow. It ensures that the deployed code is always in...

Synonym Word Replacer

Create a Synonym Word Replacer with JavaScript

Writing compelling and engaging content often requires finding the perfect words to convey our thoughts. However, occasionally we find ourselves...

Next Post
Add NWS Weather to Your Website

Add NWS Weather to Your Website

Recommended Services

Latest Articles

My Backup Setup for Linux PCs

My Backup Setup for Linux PCs

March 31st is World Backup Day. It is meant to be a reminder, but I do not rely on reminders...

Read moreDetails

Detecting Hidden WordPress Malware Disguised as Images

Detecting Hidden WordPress Malware Disguised as Images

With the recent discovery of malicious files like Stained_Heart_Red-600x500.png being found on servers across the web, it pushed me to...

Read moreDetails

Server-Side Image Conversion with Apache

Server-Side Image Conversion with Apache

I stopped relying on third party image services a while ago. They work, but they add cost, latency, and another...

Read moreDetails

Imposter Syndrome as a Self-Taught Developer

Imposter Syndrome as a Self-Taught Developer

I started writing code over 35 years ago. Everything I learned came from figuring things out on my own, long...

Read moreDetails
  • Privacy Policy
  • Terms of Service

© 2025 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Tutorials
    • Privacy
    • Security
  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity