DEV Community

Cover image for How to create a Google Maps clone using OpenStreetMap API

Posted on


How to create a Google Maps clone using OpenStreetMap API

Recently, I have been interested in making some projects such as a MERN stack Zillow clone, or a Django travel booking site. However, in researching the tools needed to create both of these, one critical piece was always too expensive: the google maps api. This API is billed per request, so if you are attaching it to a client application that end users can interact with, this will potentially drain your bank account (without proper captcha & security). The solution to this, instead, is to use a free and open source location data API. You can also follow along with this guide with the full Github source code.

What we are building
Instead of relying on a paid service, this project uses completely free tools. It is built with the following.

  • Node.js
  • Express.js (server)
  • EJS (JavaScript templates)
  • OpenStreetMap (Location data API)
  • Leaflet.js (Interactive map widget)

Getting started
To start, create a new folder, and open a command line in the directory. From there, initialize a new npm / node.js project.

npm init
Enter fullscreen mode Exit fullscreen mode

Now, install the required dependencies. We are using express for the web server and EJS (JavaScript HTML templates) for the frontend. Also install nodemon so we don't have to refresh the server after every change.

npm install express ejs
npm install --save-dev nodemon
Enter fullscreen mode Exit fullscreen mode

Lastly, open package.json and replace "scripts" with the following.

"scripts": {
    "dev": "nodemon server.js",
    "start": "node server.js"
Enter fullscreen mode Exit fullscreen mode

Building the server
Now, we are ready to build the express.js server to render our site. Copy and paste the following into a new file in the root of your project named "server.js".

const express = require("express");
const app = express()

app.set("view engine", "ejs")
app.use(express.urlencoded({ extended: false }))
app.use(express.static(__dirname + '/static'));

app.get("/", (req, res) => {

app.listen(process.env.PORT || 8000)
Enter fullscreen mode Exit fullscreen mode

This gives us a basic express server that renders our index page. It is also setup to load CSS, images, and JavaScript from a folder named "static" in the root as well.

Adding the frontend
Our web server should be running, however, there is no index file to render yet. Create a file named "index.ejs" inside a new folder named "views". Then, type "!" and hit enter (if your using VS code) to create a new HTML file. Paste the following into the body of your HTML file.

<div class="map-container">
        <form id="search-form" class="nav-bar">
            <h1 class="nav-logo">
                Maps Clone
            <input type="text" class="search" name="searchTerm" required />
            <button type="submit" class="submit-btn">
    <div id="map"></div>
Enter fullscreen mode Exit fullscreen mode

Lastly, we need to add some basic styling. Create a file named "style.css" in /static/css. Then, import your style sheet, and paste in these styles from the Github reposory.

Adding the map
We have an HTML div element with an id of map, but there is no actual map being rendered yet. To render the map, paste the CSS and script import for leaflet JS into the head of index.ejs.

<link rel="stylesheet" href="" integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI=" crossorigin="" />
<script src="" integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=" crossorigin=""></script>
Enter fullscreen mode Exit fullscreen mode

Last but not least, paste the following code into a new JavaScript file in /static/js/maplogic.js.

var map ='map').setView([33.659541, -118.1552947], 9)

L.tileLayer('{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; <a href="">OpenStreetMap</a>'

const updateMap = (coordinates) => {
    map.setView([coordinates[0], coordinates[2]], 9)

document.getElementById("search-form").addEventListener('submit', (event) => {
    const searchTerm =
    fetch(("" + searchTerm), {
        method: "GET",
        headers: {
    }).then((response) => response.json())
    .then((responseJson) => {
        if(responseJson[0]) {
            const coordinates = responseJson[0].boundingbox
        } else {
            alert("No locations found.")
    .catch((error) => {
Enter fullscreen mode Exit fullscreen mode

This renders out the map using the Leaflet script we imported, as well as sets up an event listener to re-render the map when we search for a location.

I hope you enjoy this map project as much as I did making it. I think these open source map libraries have huge potential, and I hope you can get this project running locally to test them out. If you want to download the full source code for this project the repo can be found on Github here.

If you have any questions, feel free to contact me directly on here, or on Twitter.

Top comments (0)

Super Useful CSS Resources

A collection of 70 hand-picked, web-based tools which are actually useful.
Each will generate pure CSS without the need for JS or any external libraries.