• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Creating A Command-Line Python Radio Player

    Creating a Command-Line Python Radio Player

    Malware Detection With Maldet

    Malware Detection with Maldet

    Understanding And Implementing Clamav On Linux

    Understanding and Implementing ClamAV on Linux

    Simplifying Sms Alerts With Python And Smtp

    Simplifying SMS Alerts with Python and SMTP

    Automating Javascript Minification With Php

    Automating JavaScript Minification with PHP

    Secure Password Hashing

    Secure Password Hashing with PHP

    Synonym Word Replacer

    Create a Synonym Word Replacer with JavaScript

    Converting Images To Webp With Php

    Converting Images to WebP with PHP

    Verifying Artist Names In Mp3 Files Using The Last.fm Api With Python

    Verifying Artist Names in MP3 Files Using the Last.fm API with Python

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Python
    • Security
    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

    38 Essential Points To Harden Wordpress

    38 Essential Points to Harden WordPress

    10 Tips And Tricks To Secure Your Wordpress Website

    10 Tips and Tricks to Securing Your WordPress Website

    Ai And Coding: Will Ai Replace Coders?

    AI and Coding: Will Artificial Intelligence Replace Coders?

    Creating Strong Passwords

    Creating Strong Passwords: Factors, Best Practices, and Mistakes to Avoid

    Secure Your Online Life With Bitwarden

    Secure Your Online Life with Bitwarden

    Install Python 3 On Windows 10

    How to Install Python 3 on Windows 10

  • Services
    • WordPress Care Plans
    • WordPress Technical Support
  • Apps
    • Bible App
    • Blue AI Chatbot
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Creating A Command-Line Python Radio Player

    Creating a Command-Line Python Radio Player

    Malware Detection With Maldet

    Malware Detection with Maldet

    Understanding And Implementing Clamav On Linux

    Understanding and Implementing ClamAV on Linux

    Simplifying Sms Alerts With Python And Smtp

    Simplifying SMS Alerts with Python and SMTP

    Automating Javascript Minification With Php

    Automating JavaScript Minification with PHP

    Secure Password Hashing

    Secure Password Hashing with PHP

    Synonym Word Replacer

    Create a Synonym Word Replacer with JavaScript

    Converting Images To Webp With Php

    Converting Images to WebP with PHP

    Verifying Artist Names In Mp3 Files Using The Last.fm Api With Python

    Verifying Artist Names in MP3 Files Using the Last.fm API with Python

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Python
    • Security
    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

    38 Essential Points To Harden Wordpress

    38 Essential Points to Harden WordPress

    10 Tips And Tricks To Secure Your Wordpress Website

    10 Tips and Tricks to Securing Your WordPress Website

    Ai And Coding: Will Ai Replace Coders?

    AI and Coding: Will Artificial Intelligence Replace Coders?

    Creating Strong Passwords

    Creating Strong Passwords: Factors, Best Practices, and Mistakes to Avoid

    Secure Your Online Life With Bitwarden

    Secure Your Online Life with Bitwarden

    Install Python 3 On Windows 10

    How to Install Python 3 on Windows 10

  • Services
    • WordPress Care Plans
    • WordPress Technical Support
  • Apps
    • Bible App
    • Blue AI Chatbot
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
No Result
View All Result
Home Guides Programming JavaScript

Integrate What3Words with OpenStreetMap

Jonathan Moore by Jonathan Moore
2 years ago
Reading Time: 5 mins read
A A
Integrate What3Words With Openstreetmap
FacebookTwitter

In my previous OpenStreetMap guide, I showed you how to add a map to your website by using OpenStreetMap and the Leaflet.js library, which prevented you from having to register for a Google Maps API key. In this guide, I am going to show you how to integrate the What3Words API into the markers of your map.

What3Words divided the world into 10-foot squares and gave each square a unique combination of three words, which makes it easy to find and share exact locations.

Before we can get started, you will need to create a free account on the What3Words website and create a new API key to use. Since we will only be converting coordinates to three words and not three words to coordinates, we will have unlimited free usage of the API.

Let's Get Started

Instead of reinventing the wheel, we are going to start off with the finished code from our OpenStreetMap guide and modify it for the new integration.

<!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/leaflet@1.7.1/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/leaflet@1.7.1/dist/leaflet.js"></script>

   </body>
</html>

Add a Link Style to CSS

This is optional but will help improve the look of the W3W link that appears in the marker.

body {
    margin: 0;
}
#map {
    width: 640px;
    height: 480px;
    background-color: grey;
}
a {
    text-decoration: none;
}

Adding the What3Words (W3W) Function

We need to modify our script tag and add a new function that will be used to request a conversion of the latitude and longitude to three words. Replace the YOUR_API_KEY value with the API key that you generated earlier.

async function w3w( lat, lng ) {
    return fetch( `https://api.what3words.com/v3/convert-to-3wa?coordinates=${lat},${lng}&language=en&key=YOUR_API_KEY` )
    .then( async response => {
        const json = await response.json();
        return await `<span style="color: #e11f26;">///</span><a href="https://what3words.com/${json.words}" target="_blank">${json.words}</a>`;
    });
}

Modify the create_markers Function

There is one last thing that we will need to do before we are finished, we will need to make the create_markers function asynchronous, add a call to the new w3w function to retrieve the data, and add that data to the marker popup text.

async 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++ ) {
        let data = await w3w( markers[i][1], markers[i][2] );
        marker = new L.marker( [ markers[i][1], markers[i][2] ] )
            .bindPopup( markers[i][0] + "<br /><br />" + data )
            .addTo( map );
    }
}

Our Final Project

Now that we have finished modifying our code to include the new integration, we can see what the finished application will 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/leaflet@1.7.1/dist/leaflet.css" />
        <style>
            body {
                margin: 0;
            }
            #map {
                width: 640px;
                height: 480px;
                background-color: grey;
            }
            a {
                text-decoration: none;
            }
        </style>
        <title>My First OpenStreetMap</title>
   </head>

   <body onload="initMap()">

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

        <script>
            var map;

            async function w3w( lat, lng) {
                return fetch( `https://api.what3words.com/v3/convert-to-3wa?coordinates=${lat},${lng}&language=en&key=YOUR_API_KEY` )
                .then( async response => {
                    const json = await response.json();
                    return await `<span style="color: #e11f26;">///</span><a href="https://what3words.com/${json.words}" target="_blank">${json.words}</a>`;
                });
            }

            async 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++ ) {
                    let data = await w3w(markers[i][1], markers[i][2]);
                    marker = new L.marker( [ markers[i][1], markers[i][2] ] )
                        .bindPopup( markers[i][0] + "<br /><br />" + data )
                        .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/leaflet@1.7.1/dist/leaflet.js"></script>

   </body>
</html>

We have completed the guide for integrating What3Words into our OpenStreetMap. If you have done everything correctly, your page should display a map similar to the one below. Click on each marker to see the integration.

Tags: JavaScriptLeafletMappingOpenStreetMapWhat3Words
ShareTweetSharePinShareShareScan
ADVERTISEMENT
Jonathan Moore

Jonathan Moore

Senior Software Engineer and Cybersecurity Specialist with over 3 decades of experience in developing web, desktop, and server applications for Linux and Windows-based operating systems. Worked on numerous projects, including automation, artificial intelligence, data analysis, application programming interfaces, intrusion detection systems, streaming audio servers, WordPress plugins, and much more.

Related Articles

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...

Create A Pomodoro Timer In Javascript

Create a Pomodoro Timer in JavaScript

Are you often overwhelmed by your workload and find it challenging to stay focused? Do you frequently procrastinate or lose...

Next Post
Send Email With Python

Send Email with Python

Recommended Services

Subscribe for Updates

Would you like to be notified when a new article is published? You can unsubscribe at any time.

Subscribe

Latest Articles

Understanding Man-in-the-Middle Attacks

Understanding Man-In-The-Middle Attacks

Man-in-the-middle (MitM) attacks have earned a notorious reputation as one of the most insidious and potentially devastating cybersecurity threats today....

Read more

Creating a Command-Line Python Radio Player

Creating A Command-Line Python Radio Player

In this tutorial, I'll walk you through the process of creating a command-line radio player using Python and ffplay, a...

Read more

Malware Detection with Maldet

Malware Detection With Maldet

Malware, short for malicious software, is a ubiquitous threat to all computer systems. It's the covert intruder that sneaks into...

Read more

Understanding and Implementing ClamAV on Linux

Understanding And Implementing Clamav On Linux

The perpetual arms race between threat actors and defenders often hinges on the tools each employs. For Linux users, one...

Read more
473 Spam Attacks blocked with CleanTalk
  • Privacy Policy
  • Terms of Service

© 2023 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Python
    • Privacy
    • Security
  • Services
    • Privacy Focused Web Analytics
    • WordPress Care Plans
    • WordPress Technical Support
  • Apps
    • Bible App
    • Blue AI Chatbot
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact