<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>direct distance &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/direct-distance/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 30 Mar 2024 14:05:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to compute distance between 2 GPS points?</title>
		<link>https://codepills.com/compute-distance-2-gps-points/</link>
					<comments>https://codepills.com/compute-distance-2-gps-points/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Thu, 15 Dec 2016 18:05:03 +0000</pubDate>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[direct distance]]></category>
		<category><![CDATA[distance between airports]]></category>
		<category><![CDATA[Earth's surface]]></category>
		<category><![CDATA[geographical calculations]]></category>
		<category><![CDATA[Great Circle distance]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://codekopf.com/?p=750</guid>

					<description><![CDATA[This is an algorithm for measuring distance between 2 GPS points.  <a href="https://codepills.com/compute-distance-2-gps-points/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>When working on projects involving geographical calculations, such as determining the direct distance between two airports on a global scale, developers often seek efficient methods to compute such distances accurately. One common approach involves utilizing algorithms derived from the Great Circle distance method, which considers the curvature of the Earth&#8217;s surface.</p>
<p><span id="more-750"></span></p>
<p>In this article, we explore a PHP implementation of this method and propose two simplifications to enhance its efficiency and readability.</p>
<h2>Introduction</h2>
<p>In one of my projects, I was looking for a simple way to compute the direct distance between 2 arbitrary airports in the world.</p>
<p>I was looking for a way to measure the distance between 2 GPS points. Naturally, there are several ways to compute such distances.</p>
<p>After research, I found that many people use algorithms derived from the Great circle distance method [<a href="https://en.wikipedia.org/wiki/Great-circle_distance" title="Great-circle distance" target="_blank" rel="nofollow noopener">wiki</a>].</p>
<p><!--more--></p>
<p>Here is a PHP code:</p>
<pre><code class="language-php">/**
 * Great Circle distance algorithm
 *
 * Notes:
 * - South and West locations are negative, North and East are positive.
 * - Automatically transfer degrees to radians
 * - https://en.wikipedia.org/wiki/Great-circle_distance
 *
 * @param lat1 - latitude of the first point on the sphere
 * @param lon1 - longitude of the first point on the sphere
 * @param lat2 - latitude of the second point on the sphere
 * @param lon2 - longitude of the second point on the sphere
 * @param unit - units for output (miles, kilometers, nautical miles)
 * @return - distance in desired distance units
 *
 */
function GreatCircleDistance($lat1, $lon1, $lat2, $lon2, $unit) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    switch ($unit) {
        case 'MI':
            return $miles;
            break;
        case 'KM':
            return ($miles * 1.609344);
            break;
        case 'NM':
            return ($miles * 0.8684);
            break;
    }
}</code></pre>
<p>There are two simple corrections to simplify this algorithm.</p>
<p>First by padding all the sub-results into one big equation.</p>
<p>Second, by ensuring the input units will always be the same (using numbers instead of Strings), the <code>strtoupper()</code> function can be removed.</p>
<h2>Conclusion</h2>
<p>In conclusion, by streamlining the implementation of the Great Circle distance algorithm in PHP, developers can achieve enhanced efficiency and readability in their geographical computations. These simplifications make the code more concise and ensure consistent input handling, contributing to a smoother development process.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/compute-distance-2-gps-points/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
