Distance Sphere Calculator for MariaDB Users


MariaDB Distance Sphere Calculator

Calculate the great-circle distance between two points on a sphere, replicating the logic behind MariaDB’s ST_Distance_Sphere function. Perfect for developers and data analysts working with geospatial data.


E.g., for London: 51.5074


E.g., for London: -0.1278


E.g., for Paris: 48.8566


E.g., for Paris: 2.3522


Select the desired output unit for the distance.


Calculation Results


Great-Circle Distance

Intermediate Values

Earth Radius Used: --

Central Angle (radians): --

Distance in All Units
Unit Distance
Kilometers (km)
Miles (mi)
Nautical Miles (nmi)

Comparison of Distances by Unit

What is `calculate distance sphere using mariadb`?

To “calculate distance sphere using mariadb” refers to using MariaDB’s built-in geospatial functions, specifically ST_Distance_Sphere(), to compute the shortest distance between two points on the surface of a sphere. This is also known as the great-circle distance. It’s a critical function for applications involving location data, such as finding nearby points of interest, calculating shipping routes, or analyzing geographic data sets. The calculator above simulates this exact functionality.

Unlike simple Pythagorean distance on a flat plane, this calculation accounts for the Earth’s curvature, providing accurate results over long distances. MariaDB uses a spherical model of the Earth, which is a very good approximation for most applications.

The MariaDB Formula and Explanation

In MariaDB, you don’t need to write the complex Haversine formula by hand. You use the powerful `ST_Distance_Sphere()` function.

MariaDB Syntax

SELECT ST_Distance_Sphere(point1, point2, [radius])

This function returns the distance in **meters** by default.

Underlying Logic: The Haversine Formula

The ST_Distance_Sphere function is an implementation of the Haversine formula, which is designed to calculate great-circle distances. The formula is:

a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)

c = 2 ⋅ atan2(√a, √(1−a))

d = R ⋅ c

Formula Variables
Variable Meaning Unit Typical Range
φ1, φ2 Latitude of point 1 and point 2 Degrees (converted to Radians for calculation) -90 to +90
λ1, λ2 Longitude of point 1 and point 2 Degrees (converted to Radians for calculation) -180 to +180
R Mean radius of the Earth Meters, Kilometers, or Miles ~6,371 km
d The resulting distance Same as Radius unit 0 to ~20,000 km

Practical MariaDB Examples

Here’s how you would actually perform these calculations inside a MariaDB database. These examples assume you have a table with location data.

Example 1: Basic Distance Calculation

Calculate the distance between London and Paris in meters using MariaDB’s default Earth radius (6,371,000 meters).

SELECT ST_Distance_Sphere(
    POINT(-0.1278, 51.5074), 
    POINT(2.3522, 48.8566)
) AS distance_in_meters;

-- Result: 343971.55... (approx)

Example 2: Finding Locations Within a Radius

Imagine a table `cities` with a `location` POINT column. Let’s find all cities within 500 kilometers of Berlin (13.4050° E, 52.5200° N). We can use this information in our geospatial analysis.

SELECT name, 
       ST_Distance_Sphere(POINT(13.4050, 52.5200), location) / 1000 AS distance_km
FROM cities
WHERE ST_Distance_Sphere(POINT(13.4050, 52.5200), location) <= 500000
ORDER BY distance_km;

How to Use This MariaDB Distance Sphere Calculator

  1. Enter Point 1 Coordinates: Input the latitude and longitude for your starting location into the 'Point 1' fields.
  2. Enter Point 2 Coordinates: Input the latitude and longitude for your destination into the 'Point 2' fields.
  3. Select Units: Choose whether you want the final distance to be calculated in Kilometers, Miles, or Nautical Miles. The calculator automatically adjusts the Earth radius for the selected unit.
  4. View Results: The calculator instantly updates, showing the primary result, a table with all units, and a visual chart. The values mirror what a `calculate distance sphere using mariadb` query would return, just converted to your chosen units.
  5. Reset: Use the "Reset" button to return to the default values (London to Paris).

Key Factors That Affect Distance Calculation

  • Earth's Radius: The default radius in MariaDB is 6,371,000 meters. Using a different radius will change the result. Our calculator uses standard values for each unit system.
  • Coordinate Precision: The more decimal places in your latitude/longitude values, the more precise the distance calculation will be.
  • Spherical vs. Ellipsoidal Model: `ST_Distance_Sphere` assumes a perfect sphere. For hyper-accurate surveying, a more complex ellipsoidal model (like WGS84) is needed, but for 99% of applications, the spherical model is excellent. You can learn about this in our GIS data types guide.
  • Data Types in MariaDB: Storing your coordinates in the `POINT` data type is crucial for performance and functionality. This is a core part of any database schema design involving maps.
  • Longitude/Latitude Order: Be careful! Many systems use (Latitude, Longitude), but MariaDB's `POINT` constructor expects `POINT(Longitude, Latitude)`. Getting this wrong is a common bug.
  • Database Indexing: For large datasets, a spatial index on your `POINT` column is essential for fast queries. Learn about this in our performance tuning guide.

Frequently Asked Questions (FAQ)

1. Why is the result different from Google Maps?

Google Maps often calculates driving or walking distance, not the direct "as the crow flies" distance. This calculator provides the great-circle distance, which is the shortest path on the globe's surface.

2. What is the `POINT` data type in MariaDB?

It's a special data type for storing a single location in a two-dimensional space, perfect for holding longitude and latitude pairs.

3. Is ST_Distance_Sphere available in all MariaDB versions?

No, it was introduced in later versions (around 10.2+). Older versions might require a user-defined function to perform this calculation.

4. How do I convert the result to miles in SQL?

Since the function returns meters, you can convert it by multiplying. `(ST_Distance_Sphere(...) / 1609.34)` will give you the approximate distance in miles.

5. What is a "great-circle distance"?

It's the shortest distance between two points on the surface of a sphere. It's the path you'd follow if you stretched a string between two points on a globe. For more details, see our article on spherical geometry.

6. Does this calculator actually use a MariaDB database?

No, this is a frontend tool written in JavaScript that precisely mimics the mathematical formula used by MariaDB's function. It allows you to `calculate distance sphere using mariadb` logic without needing a database connection.

7. Why is the longitude first in `POINT(lon, lat)`?

This follows the OGC (Open Geospatial Consortium) standard, which specifies coordinates as (x, y), corresponding to (longitude, latitude).

8. Can I use a custom Earth radius?

In MariaDB, yes, by passing it as a third argument. This calculator uses industry-standard mean radii for consistency.

© 2026 SEO Tools Inc. This tool is for educational purposes. Always verify critical calculations.


Leave a Reply

Your email address will not be published. Required fields are marked *