Distance Calculation Using Latitude and Longitude in PHP – Live Calculator


Distance Calculation Using Latitude and Longitude in PHP

A smart calculator to find the great-circle distance between two points on Earth.

Geographical Distance Calculator


Enter the latitude of the first point in decimal degrees.
Latitude must be between -90 and 90.


Enter the longitude of the first point in decimal degrees.
Longitude must be between -180 and 180.


Enter the latitude of the second point in decimal degrees.
Latitude must be between -90 and 90.


Enter the longitude of the second point in decimal degrees.
Longitude must be between -180 and 180.



What is a Latitude and Longitude Distance Calculation?

A latitude and longitude distance calculation determines the distance between two points on the surface of the Earth. This isn’t a simple straight line on a flat map; it’s the shortest distance along the planet’s curve, known as the “great-circle distance”. This calculation is fundamental for applications in navigation, logistics, geography, and any web service that deals with location data. While our calculator works in your browser, the core logic is often needed on a server, which is why a distance calculation using latitude and longitude in PHP is a common requirement for developers building location-aware applications.

This calculator is for anyone from students learning about geography to developers needing to implement a PHP geo distance function. A common misunderstanding is that one can simply use the Pythagorean theorem, but this fails because it doesn’t account for the Earth’s curvature. For accurate results, spherical trigonometry is required.

The Haversine Formula for Distance Calculation

The most common method for calculating the great-circle distance is the Haversine formula. It’s a reliable choice that provides accurate results for most applications, although it assumes the Earth is a perfect sphere. The formula is:

a = sin²(Δφ/2) + cos(φ1) * cos(φ2) * sin²(Δλ/2)
c = 2 * atan2(√a, √(1−a))
d = R * c

To perform a distance calculation using latitude and longitude in PHP, you would implement this formula as a function. Here is a sample implementation:

<?php
function haversineGreatCircleDistance(
  $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371)
{
  // convert from degrees to radians
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $latDelta = $latTo - $latFrom;
  $lonDelta = $lonTo - $lonFrom;

  $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
    cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
  return $angle * $earthRadius;
}
?>

Formula Variables

Variables used in the Haversine formula.
Variable Meaning Unit Typical Range
φ1, λ1 Latitude and Longitude of Point 1 Degrees -90 to +90 (lat), -180 to +180 (lon)
φ2, λ2 Latitude and Longitude of Point 2 Degrees -90 to +90 (lat), -180 to +180 (lon)
Δφ, Δλ Difference in latitude and longitude Radians Varies
R Earth’s mean radius km / mi ~6,371 km or ~3,959 mi
d Calculated distance km / mi / nmi 0 to ~20,000 km

Practical Examples

Example 1: New York to London

  • Input (Point 1): Latitude 40.7128°, Longitude -74.0060°
  • Input (Point 2): Latitude 51.5074°, Longitude -0.1278°
  • Result (km): Approximately 5,570 km
  • Result (mi): Approximately 3,461 miles

This shows the power of a good haversine formula calculator for long-distance travel planning.

Example 2: Sydney to Tokyo

  • Input (Point 1): Latitude -33.8688°, Longitude 151.2093°
  • Input (Point 2): Latitude 35.6895°, Longitude 139.6917°
  • Result (km): Approximately 7,825 km
  • Result (mi): Approximately 4,862 miles

How to Use This Distance Calculator

  1. Enter Coordinates: Input the latitude and longitude for both Point 1 and Point 2 in the designated fields. Use negative values for South latitudes and West longitudes.
  2. Select Units: Choose your desired output unit for the distance (Kilometers, Miles, or Nautical Miles) from the dropdown menu.
  3. Calculate: Click the “Calculate Distance” button.
  4. Interpret Results: The primary result shows the calculated distance in your selected unit. The intermediate results provide more context, and the chart visualizes the distance across different units. You can learn more about coordinate systems with our article on what is WGS84.

Key Factors That Affect Distance Calculation

  • Earth’s Shape: The Haversine formula assumes a perfect sphere, but Earth is an oblate spheroid (slightly flattened at the poles). This can introduce an error of up to 0.5%.
  • Formula Choice: For higher precision, Vincenty’s formulae are used, which work on an ellipsoid model. See a comparison at Vincenty vs Haversine.
  • Coordinate Precision: The more decimal places in your input coordinates, the more precise the final calculation will be.
  • Altitude: The standard formula calculates distance at sea level. For calculations involving significant altitude differences (e.g., aviation), this factor must be considered.
  • Unit of Measurement: Using an incorrect Earth radius for your chosen unit will lead to wrong results. This calculator handles the conversion automatically. For more on units, try our decimal to DMS converter.
  • Short Distances: For very short distances, the Haversine formula can have rounding errors. Simpler methods that treat the surface as flat over a small area may be more stable.

Frequently Asked Questions (FAQ)

1. Why is the result different from Google Maps?
Google Maps uses a more complex model based on an ellipsoid (like WGS84) and also calculates routing along roads, not just the direct “as the crow flies” distance. Our calculator provides the great-circle distance, which is the shortest path on the sphere’s surface.
2. What are latitude and longitude?
Latitude measures how far north or south of the equator a point is (from -90° to +90°). Longitude measures how far east or west of the Prime Meridian a point is (from -180° to +180°).
3. How can I get coordinates for a location?
You can use online mapping services like Google Maps. Right-clicking on a location will typically show its latitude and longitude.
4. What is the most accurate formula for distance calculation?
Vincenty’s formulae are generally considered more accurate than Haversine because they work on an ellipsoid model of the Earth, but they are more computationally intensive.
5. Can I use this for my PHP application?
Absolutely. The PHP code example provided above is a great starting point for integrating a distance calculation using latitude and longitude in PHP into your projects.
6. What’s the difference between a mile and a nautical mile?
A statute mile is 5,280 feet. A nautical mile is based on the Earth’s circumference and is equal to one minute of latitude, which is approximately 6,076 feet or 1.15 statute miles.
7. Why do I need to convert degrees to radians?
Trigonometric functions in most programming languages, including PHP and JavaScript, expect angles to be in radians, not degrees. The conversion is a critical step for the formula to work correctly.
8. Does this calculator work for any two points on Earth?
Yes, it works for any two points as long as you provide valid latitude and longitude coordinates. It correctly handles crossing the equator and the prime meridian.

© 2026 GeoCalculators. All rights reserved.



Leave a Reply

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