2012-03-08 67 views
0

我卡住了,需要一些幫助。我有一個擁有3000多個緯度和經度的數據庫,我試圖將它們轉換爲十進制經度和緯度,這樣我就可以顯示兩個座標之間的距離。這兩個座標在頁面的url中傳遞。第一個(lat1 & lon1)已經轉換,但第二個(lat2 & lon2)不是。我的數據庫中的緯度和經度存儲如下:26°56.34308'-094°41.32328',所以我認爲這些逗號可能會被刪除。我從幾個來源獲得了一些代碼,但我不確定如何正確地將它們組合在一起。需要幫助轉換和計算距離

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

是否有任何錯誤發生的特定行? – jmishra 2012-03-08 04:13:03

+0

您已打開此問題的確切副本。 – Josh 2012-03-08 05:46:45

+0

檢查此鏈接http://code.google.com/apis/maps/documentation/distancematrix/ – 2012-03-08 06:08:47

回答

0

您有數據庫中的緯度和經度嗎?什麼樣的數據庫?一些數據庫如MySQL和PostgresQL可以輕鬆計算簡單SQL查詢中2點之間的距離。爲了做到這一點,您必須確保您的數據位於空間列中,如point列。 (如果您沒有point列中的數據,則可以輕鬆創建新列,並使用非點列中的數據更新新列)

然後,要計算2個點之間的距離,只需執行一個SQL查詢。

<?php 

$dbh = mysql_connect($host, $username, $password); 
mysql_select_db($dbname); 

// Get distance between 2 points manually 
$query = "select GLength(GeomFromText('LineString({$lat1} {$lon1},{$lat2} {$lon2})'))"; 
$dbr = mysql_query($query); 
list($length) = mysql_fetch_array($dbr); 
// Now, $length has the distance between lat1, lon1 and lat2, lon2 

// To put points into a table use this query 
// Assuming that your table has 2 columns point1 and point2 
$query = "insert into mytable (point1, point2) values(geomfromtext('point({$lat1} {$lon1})'), geomfromtext('point({$lat2} {$lon2})'))"; 
mysql_query($query); 

// Get distance between 2 points that are stored in point columns 
// Assuming the table has 2 point columns point1 and point2 
$query = "select GLength(GeomFromText(concat('LineString(', x(point1), ' ', y(point1), ',', x(point2), ' ', y(point2), ')'))) from mytable"; 
$dbr = mysql_query($query); 
list($length) = mysql_fetch_array($dbr); 
// Now $length has the distance between point1 and point2 

?> 

然後,您可以從那裏進行任何距離轉換。 (另外,在查詢中使用變量時不要像上面那樣使用$lat1$lon1等等來檢查它們,請小心謹慎。確保並檢查它們以確保它們只包含數字(如果您從用戶輸入中獲取值)。