Tiny Frontend Logo
Posts 0138

On This Page

Build a Tile-Based Google Map with JavaScript

Ever wondered how Google Maps loads and zooms so smoothly? While their full system is massively complex, the fundamental idea behind map display is surprisingly approachable.

In this post, we'll walk through the process of building a very basic tile-based map display library - just enough to understand how tiled maps work, including zoom and pan behavior.

Introducing the Tile System

A world map divided into tiles at different zoom levels (Zoom 0, Zoom 1, Zoom 2)

Modern web-based maps like Google Maps and OpenStreetMap use a tile-based system to display the world at various zoom levels. The core concept is simple:

  • The map is split into square tiles, usually 256×256 pixels.
  • At zoom level 0, the entire world fits into one tile.
  • Each higher zoom level doubles the resolution, making the grid size 2ⁿ × 2ⁿ tiles.
  • These tiles are pre-rendered images and fetched dynamically based on the user's view.

This approach allows for efficient loading, smooth panning, and zooming at different scales. The URL format for a tile is:

https://tile.openstreetmap.org/{z}/{x}/{y}.png

Where:

  • z is the zoom level
  • x and y are the tile column and row indexes at that zoom level

Calculating Tile Coordinates from Latitude and Longitude

To display a map centered at a specific location, we need to convert geographic coordinates (latitude and longitude) into tile coordinates.

Here's how it's done using the Web Mercator projection:

function latLngToTile(lat, lng, zoom) {
  const x = ((lng + 180) / 360) * Math.pow(2, zoom);
  const y =
    ((1 -
      Math.log(
        Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180)
      ) /
        Math.PI) /
      2) *
    Math.pow(2, zoom);
  return { x, y };
}

This gives you a floating-point tile coordinate. For rendering, you typically round or floor these values to get the tile index.

Subscribe to read the full content

* Free subscribe to enjoy full content access.
* You can cancel anytime with 1-click.

Read Next

0137

Mastering Frontend Performance with Lighthouse

Web performance significantly impacts user experience, search rankings, and overall site success. One indispensable tool frontend developers have at their disposal is Google Lighthouse—a powerful, open-source tool for auditing and improving web page performance, accessibility, and SEO.

Mastering Frontend Performance with Lighthouse
0136

Advanced Video Streaming like YouTube

Have you ever wondered how to build a video playback feature like YouTube, where videos seamlessly adjust quality based on your internet connection?

Advanced Video Streaming like YouTube
0135

React-Query Data Fetching

React Query, also known as TanStack Query, is a powerful library designed to simplify data fetching, caching, and state management in React applications.

React-Query Data Fetching
0134

Implementing a Custom Event Delegation Utility

This post will guide you through creating a custom utility function for event delegation in JavaScript. We'll cover the fundamentals of event delegation, explore its benefits, and build a robust and versatile utility function that you can use in your web development projects.

Implementing a Custom Event Delegation Utility
0133

Build Frontend Project with Mock Server

Ever found yourself ready to start frontend development for a new feature, only to be stalled because the backend API isn't ready? Mock server is your escape from this common development frustration.

Build Frontend Project with Mock Server
0132

Translate Your React Application

Ever wondered what i18n stands for?

Translate Your React Application