Home > Java > javaTutorial > body text

How to Calculate Distance Between Two Points on Earth Using Latitude and Longitude?

Linda Hamilton
Release: 2024-11-01 01:48:28
Original
973 people have browsed it

How to Calculate Distance Between Two Points on Earth Using Latitude and Longitude?

Introducing Latitude and Longitude-Based Distance Calculations

Calculating the distance between two points on the Earth's surface using their latitude and longitude coordinates is a fundamental task in various applications. Below, we'll explore the intricacies of calculating this distance using a snippet of Java code and delve into an improved implementation that accounts for altitude differences.

Calculating Distance Using a Snippet of Java Code

The code provided by Dommer utilizes a simplified formula to compute the distance between two points on the Earth's surface:

<code class="java">double temp = Math.cos(Math.toRadians(latA))
            * Math.cos(Math.toRadians(latB))
            * Math.cos(Math.toRadians((latB) - (latA)))
            + Math.sin(Math.toRadians(latA))
            * Math.sin(Math.toRadians(latB));
    return temp * RADIUS * Math.PI / 180;</code>
Copy after login

This formula assumes a spherical Earth and yields reasonable results for small distances. However, it fails to consider altitude differences, which can lead to inaccuracies for significant distances.

Introducing the Haversine Formula for Altitude-Aware Distance Calculation

To address the limitations of the formula used by Dommer, we turn to the Haversine formula, which incorporates a more precise model for representing the Earth's shape. This formula explicitly accounts for the distance and height difference between the two points:

<code class="java">public static double distance(double lat1, double lat2, double lon1,
        double lon2, double el1, double el2) {

    final int R = 6371; // Radius of the earth
    
    double latDistance = Math.toRadians(lat2 - lat1);
    double lonDistance = Math.toRadians(lon2 - lon1);
    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c * 1000; // convert to meters

    double height = el1 - el2;

    distance = Math.pow(distance, 2) + Math.pow(height, 2);

    return Math.sqrt(distance);
}</code>
Copy after login

This formula provides a more accurate representation of distance, making it valuable for applications requiring precise geographic calculations.

The above is the detailed content of How to Calculate Distance Between Two Points on Earth Using Latitude and Longitude?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!