MariaDB Distance Calculator | Calculate Geospatial Distance


MariaDB Geospatial Distance Calculator

Simulate MariaDB’s ST_Distance function by calculating the geodesic distance between two points on Earth. Enter the latitude and longitude for two points to get started.



Enter latitude in decimal degrees (e.g., 48.8566 for Paris).

Please enter a valid number between -90 and 90.



Enter longitude in decimal degrees (e.g., 2.3522 for Paris).

Please enter a valid number between -180 and 180.



Enter latitude in decimal degrees (e.g., 51.5074 for London).

Please enter a valid number between -90 and 90.



Enter longitude in decimal degrees (e.g., -0.1278 for London).

Please enter a valid number between -180 and 180.



Select the unit for the final calculated distance.

Distance Comparison Chart

A visual comparison of distances in different units.

What is MariaDB Distance Calculation?

To calculate distance using MariaDB is to leverage its powerful spatial extensions, which allow the database to store, index, and query geographic data. The core of this functionality is the ST_Distance() function. This function calculates the shortest distance between two spatial objects, which can be points, lines, or polygons. When used with geographic coordinates (latitude and longitude) on a spherical model of the Earth, it computes the geodesic distance—the shortest path along the surface of the globe.

This capability is crucial for a wide range of applications, from logistics and location-based services to geographic information systems (GIS) and data analysis. Instead of pulling raw coordinates into an application and performing complex calculations there, you can offload the work directly to the database, which is often faster and more efficient, especially when dealing with large datasets. For more information on database efficiency, see our guide on MariaDB performance tuning.

The ST_Distance Formula and Explanation

For geographic coordinates, MariaDB’s ST_Distance function uses the Haversine formula to calculate the great-circle distance between two points. This formula treats the Earth as a perfect sphere, which is a very close approximation for most applications. The formula is:

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

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

d = R ⋅ c

This formula is fundamental to any system that needs to calculate distance using MariaDB or similar geospatial databases.

Variables in the Haversine Formula
Variable Meaning Unit Typical Range
φ₁, φ₂ Latitude of point 1 and point 2 Radians -π/2 to +π/2
λ₁, λ₂ Longitude of point 1 and point 2 Radians -π to +π
Δφ, Δλ Difference in latitude and longitude Radians
R Radius of Earth km, miles ~6,371 km or ~3,959 miles
d The final distance between the two points km, miles 0 to ~20,000 km

Practical Examples

Example 1: New York to Los Angeles

Let’s calculate the distance between JFK Airport in New York and LAX Airport in Los Angeles.

  • Input (Point 1 – JFK): Latitude: 40.6413, Longitude: -73.7781
  • Input (Point 2 – LAX): Latitude: 33.9416, Longitude: -118.4085
  • Unit: Miles

Using the calculator, the result is approximately 2,475 miles. The corresponding SQL query would be:

SELECT ST_Distance(POINT(-73.7781, 40.6413), POINT(-118.4085, 33.9416)) * 0.000621371;

Note: By default, ST_Distance on geographic coordinates returns meters, so we multiply by 0.000621371 to convert to miles.

Example 2: Sydney to Tokyo

Now, let’s find the distance between Sydney, Australia and Tokyo, Japan.

  • Input (Point 1 – Sydney): Latitude: -33.8688, Longitude: 151.2093
  • Input (Point 2 – Tokyo): Latitude: 35.6762, Longitude: 139.6503
  • Unit: Kilometers

The calculated distance is approximately 7,825 kilometers. This kind of calculation is explored further in our geospatial indexing deep dive, which shows how to make these queries extremely fast.

How to Use This MariaDB Distance Calculator

This tool makes it simple to simulate a MariaDB distance calculation. Follow these steps:

  1. Enter Point 1 Coordinates: Input the latitude and longitude for your starting point in the first two fields.
  2. Enter Point 2 Coordinates: Input the latitude and longitude for your destination in the next two fields.
  3. Select Units: Choose whether you want the result displayed in kilometers, miles, or meters from the dropdown menu.
  4. Calculate: Click the “Calculate Distance” button. The tool will instantly display the geodesic distance.
  5. Interpret Results: The primary result is shown prominently. Below it, you’ll find the equivalent SQL query you could run in MariaDB, the Well-Known Text (WKT) representation of your points, and the Earth radius used for the specific unit conversion. This is a great way to learn and validate your own queries. Our complete SQL functions guide can help you understand other related functions.

Key Factors That Affect Distance Calculation

When you want to calculate distance using MariaDB, several factors can influence the accuracy and performance of your queries.

  • Spatial Reference System (SRID): For global coordinates, the standard is SRID 4326 (WGS 84). Using the correct SRID is crucial for ensuring MariaDB interprets your coordinates correctly as latitude/longitude pairs.
  • Coordinate Order: A common pitfall! Geospatial standards often use (latitude, longitude), but MariaDB’s POINT constructor expects (longitude, latitude). Getting this wrong will lead to incorrect results.
  • Data Types: Storing your coordinates in a native POINT or other geometry data type is far more efficient than using two separate DECIMAL or FLOAT columns.
  • Geodesic vs. Planar Calculation: MariaDB can perform calculations on a flat plane (planar) or a sphere (geodesic). For lat/lon data, you must ensure it’s performing a geodesic calculation, which ST_Distance does by default for geographic SRIDs.
  • Spatial Indexing: For large datasets, simple distance calculations are fast. But for “find all points within X distance” queries, a SPATIAL index is absolutely essential for good performance. This is a key difference between MariaDB and competitors, as discussed in our PostGIS vs MariaDB comparison.
  • Earth Model (Sphere vs. Ellipsoid): The Haversine formula assumes a perfect sphere. For most cases, this is accurate enough. However, the Earth is technically an oblate spheroid (slightly flattened at the poles). For hyper-critical accuracy (e.g., surveying), more complex formulas are needed, but MariaDB’s implementation is sufficient for over 99% of use cases.

Frequently Asked Questions (FAQ)

1. What unit does MariaDB’s ST_Distance return by default?

When used with a geographic coordinate system (like SRID 4326), ST_Distance returns the distance in meters. You must perform a conversion if you need kilometers or miles.

2. How should I store latitude/longitude data in my MariaDB table?

The best practice is to use the POINT spatial data type. For example: ALTER TABLE locations ADD COLUMN coord POINT SRID 4326;. This is more efficient for storage and querying than using separate number columns.

3. Why are the coordinates in the POINT constructor reversed (longitude, latitude)?

This follows the OGC (Open Geospatial Consortium) standard, which specifies an (x, y) coordinate order. In the context of a world map, longitude is the x-axis and latitude is the y-axis.

4. Is this calculator’s result 100% identical to MariaDB?

It is extremely close. Both this calculator and MariaDB use the Haversine formula, which models the Earth as a sphere. The results should match to many decimal places, with any tiny variance due to floating-point arithmetic differences in JavaScript vs. the database engine.

5. How can I find all records within a certain radius of a point?

You don’t use ST_Distance in the WHERE clause for this, as it’s not index-friendly. The efficient way is to create a buffer and find points that fall within it: ST_Contains(ST_Buffer(center_point, distance_in_meters), location_column). Using a tool from our list of web developers tools can help test these queries.

6. Does a SPATIAL index help a simple two-point distance calculation?

No. An index is for searching through many rows. A calculation between two known points does not involve searching a table, so an index provides no benefit in that specific case.

7. Can I calculate the distance between a point and a line (e.g., a road)?

Yes. ST_Distance works between any two geometry types. You can calculate the distance between a POINT and a LINESTRING to find the closest distance from the point to the line.

8. What if my data is not on Earth (e.g., a 2D game map)?

In that case, you would use a planar SRID (or SRID 0). The distance calculation then becomes a simple Pythagorean theorem calculation, and the result is in the same units as your coordinate system.

© 2026 Your Company. All Rights Reserved. A tool for developers and SEOs.



Leave a Reply

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