Note that there are some explanatory texts on larger screens.

plurals
  1. POneed help converting and calculating distance
    primarykey
    data
    text
    <p>I'm stuck and need some help. I have a database that has over 3000 latitudes and longitudes and I'm trying to convert them to decimal lat and lon so I can display the distance between two coordinates. The two coordinates are passed in the url of the page. The first (lat1 &amp; lon1) is already converted but the second (lat2 &amp; lon2) is not. The latitude and Longitude in my database are stored like This: 26°56.34308' -094°41.32328' so I'm thinking that the commas should probably be removed. I've got some code from a few sources but I'm not sure how to put them together properly.</p> <pre><code>///// Get the two locations from the url $lat1 = $_GET[lat1]; $lon1 = $_GET[lon1]; ////// lat2 &amp; Lon2 are the ones that need to be converted $lat2 = $_GET[lat2]; $lon2 = $_GET[lon2]; ///// Convert lat2 &amp; lon2 into decimal format $pos1 = strrpos($mystring, "°"); $pos2 = strrpos($mystring, "."); $pos3 = strrpos($mystring, "'"); // Get subsring from a string: substr(source, start, length) $deg = substr($mystring, 0, $pos1); $min = substr($mystring, $pos1, $pos2 - $pos1); $sec = substr($mystring, $pos2, $pos3 - $pos2); function DMStoDEC($deg,$min,$sec) { // Converts DMS ( Degrees / minutes / seconds ) // to decimal format longitude / latitude return $deg+((($min*60)+($sec))/3600); } //////calculate the distance function distance($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); if ($unit == "K") { return ($miles * 1.609344); } else if ($unit == "N") { return ($miles * 0.8684); } else { return $miles; } } // Miles echo distance($lat1, $lon1, $lat2, $lon2, "m") . " miles&lt;br&gt;&lt;br&gt;"; //Kilometers echo distance($lat1, $lon1, $lat2, $lon2, "k") . " kilometers&lt;br&gt;&lt;br&gt;"; //Nautical miles echo distance($lat1, $lon1, $lat2, $lon2, "N") . " Nautical miles"; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload