InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Finding the Distance Between Two Places in PHP
PHP

Finding the Distance Between Two Places in PHP

InfinityCoder November 17, 2016

You want to find the distance between two coordinates on planet Earth.

Use sphere_distance(), as shown inĀ Example 2-6.

Example 2-6. Finding the distance between two points

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function sphere_distance($lat1, $lon1, $lat2, $lon2, $radius = 6378.135) {
   $rad = doubleval(M_PI/180.0);
   $lat1 = doubleval($lat1) * $rad;
   $lon1 = doubleval($lon1) * $rad;
   $lat2 = doubleval($lat2) * $rad;
   $lon2 = doubleval($lon2) * $rad;
  
   $theta = $lon2 - $lon1;
   $dist = acos(sin($lat1) * sin($lat2) +
               cos($lat1) * cos($lat2) *
               cos($theta));
   if ($dist < 0) { $dist += M_PI; }
   // Default is Earth equatorial radius in kilometers
   return $dist = $dist * $radius;
}
 
// NY, NY (10040)
$lat1 = 40.858704;
$lon1 = -73.928532;
 
// SF, CA (94144)
$lat2 = 37.758434;
$lon2 = -122.435126;
$dist = sphere_distance($lat1, $lon1, $lat2, $lon2);
 
// It's about 2570 miles from NYC to SF
// $formatted is 2570.18
$formatted = sprintf("%.2f", $dist * 0.621); // Format and convert to miles

Because the Earth is not flat, you cannot get an accurate distance between two locations using a standard Pythagorean distance formula.

You must use a Great Circle algorithm instead, such as the one in sphere_distance().

Pass in the latitude and longitude of your two points as the first four arguments. The latitude and longitude of the origin come first, and then the latitude and longitude of the destination.

The value returned is the distance between them in kilometers.

The code in Example 2-6 finds the distance between New York City and San Francisco, converts the distance to miles, and then formats it to have two decimal places.

Because the Earth is not a perfect sphere, these calculations are somewhat approximate and could have an error up to 0.5%.

sphere_distance() accepts an alternative sphere radius as an optional fifth argument. This lets you, for example, discover the distance between points on Mars:

1
2
3
$martian_radius = 3397;
$dist = sphere_distance($lat1, $lon1, $lat2, $lon2, $martian_radius);
$formatted = sprintf("%.2f", $dist * 0.621); // Format and convert to miles

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Logging Errors in PHP
You want to save program errors to a log. These …

Logging Errors in PHP

Setting the Character Encoding of Outgoing Data in PHP
You want to make sure that browsers correctly handle the …

Setting the Character Encoding of Outgoing Data in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap