2013-05-13 96 views
0

我有一個地圖頁面,它具有計算距離的形式,但是這包含2個下拉列表,用於處理數據庫中的所有城市,以允許用戶從第一個下拉列表中選擇第一個點列表和第二個下拉列表中的第二個點,然後使用存儲在數據庫中的每個城市的經度和緯度來計算距離。使用mysql和php計算2點之間的距離

village table: 

- id 
- village_name 
- lattitude 
- longiude 
- district_id 

i have this query untill now but i do not know if it is right 

<?php 
    if(isset($_POST['calculate'])) 
    { 
     $sql = mysql_query("SELECT (3959 * acos(cos(radians(37)) * cos(radians(lattitude)) * cos(radians(longitude) - radians(-122)) + sin(radians(37)) * sin(radians(lat))))FROM village")or die(mysql_error()); 
    } 

?> 

的問題是,我不知道如何使用存儲在數據庫中的緯度和經度2點之間計算距離

我沒有讀到haversine公式,但我不知道如何使用它在這裏

我不想讓任何人爲我做這件事,但向我解釋並幫助我 任何人都可以幫助我嗎?

+0

作爲烏鴉飛(?智能烏鴉) – Drew 2013-05-13 21:23:24

+0

http://stackoverflow.com/questions/1006654/fastest-way-to-find-distance-between-two- lat-long-points – Drew 2013-05-13 21:24:31

+0

可能的重複:http://stackoverflow.com/questions/574691/mysql-great-circle-distance-haversine-formula – 2013-05-13 21:25:04

回答

1

在我看來,你應該使用php來計算它。

首先取緯度和從數據庫LON(我想他們在浮點格式)

$LonS - Start lon 
$LonE - End lon 
$LatS - Start lat 
$LatE - End lat 

這是大圓式 http://en.wikipedia.org/wiki/Haversine_formula

點(。)指乘法,在PHP '*'。所以,最終的代碼在PHP:

$R = 6371; //km 
$A = pow(sin(($LatE - $LatS)/2), 2) + cos($LatS) * cos($LatE) * pow(sin(($LonE - $LonS)/2) , 2); 
$C = 2 * atan2(sqrt($A), sqrt(1 - $A)); 
$D = $R * $C;