59101

Is there a way to covert latitude and longitude using php?

Question:

I have a database that contains latitudes and longitudes for over 3000 locations. In my table the latitude and longitude look like this:

latitude: 26°56.34308'<br /> longitude: -094°41.32328'

I need these numbers converted to decimal minutes and without the - and ' in the number. The reason for this is to calculate the distance to another location.

Is there a way to do this with php?

This is what I have so far and I need some help putting it all together.

///// Get the two locations from the url $lat1 = $_GET[lat1]; $lon1 = $_GET[lon1]; ////// lat2 & Lon2 are the ones that need to be converted $lat2 = $_GET[lat2]; $lon2 = $_GET[lon2]; ///// Convert lat2 & 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<br><br>"; //Kilometers echo distance($lat1, $lon1, $lat2, $lon2, "k") . " kilometers<br><br>"; //Nautical miles echo distance($lat1, $lon1, $lat2, $lon2, "N") . " Nautical miles";

Answer1:

I found this:

<?php function DMStoDEC($deg,$min,$sec) { // Converts DMS ( Degrees / minutes / seconds ) // to decimal format longitude / latitude return $deg+((($min*60)+($sec))/3600); } function DECtoDMS($dec) { // Converts decimal longitude / latitude to DMS // ( Degrees / minutes / seconds ) // This is the piece of code which may appear to // be inefficient, but to avoid issues with floating // point math we extract the integer part and the float // part by using a string function. $vars = explode(".",$dec); $deg = $vars[0]; $tempma = "0.".$vars[1]; $tempma = $tempma * 3600; $min = floor($tempma / 60); $sec = $tempma - ($min*60); return array("deg"=>$deg,"min"=>$min,"sec"=>$sec); } ?>

<a href="http://www.web-max.ca/PHP/misc_6.php" rel="nofollow">http://www.web-max.ca/PHP/misc_6.php</a>

<strong>EDIT:</strong><br /> To parse your string into deg, min, sec:

$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);

Recommend