2012-03-07 76 views
3

我有一个包含超过3000个位置的纬度和经度的数据库。在我的表中的纬度和经度是这样的:有没有办法隐藏使用php的经纬度?

北纬:26°56.34308'
经度:-094°41.32328'

我需要这些数字转换为十进制分钟,没有 - 而且'在数。这是因为计算到另一个位置的距离。

有没有办法做到这一点与PHP?

这是我到目前为止,我需要一些帮助把它放在一起。

///// 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"; 

回答

1

我发现这一点:

<?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); 
}  

?> 

http://www.web-max.ca/PHP/misc_6.php

编辑:
为了解析您的字符串转换成度,分,秒:

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

谢谢,但我我不知道如何实现,因为我的价值观是st作为一个整数编号,而不是像$ deg $ min $ sec – 2012-03-08 00:38:24

+0

看到我编辑的答案 – Caner 2012-03-08 00:46:44

+0

我想我应该提到,我真的是绿色的PHP。应该是这样的: – 2012-03-08 01:32:54

相关问题