2012-05-11 32 views
0

我有一個問題,我花了太多時間試圖解決。 我正在使用de Haversine公式來計算兩點之間的距離,但是我使用的代碼給了我的值遠高於KM等距離。 有人可以幫我嗎? 這是代碼:使用haversine公式,結果太高(固定)

$pesquisa_raio = "%" . $_GET['raio'] . "%"; 
$latitude = floatval("%" . $_GET['lat'] . "%"); 
$longitude = floatval("%" . $_GET['log'] . "%"); 
$cidade="%" . $_GET['cidade'] . "%"; 

$sql= "SELECT * FROM oferta";//"add depois" //Inner Join concelhos On oferta.concelhos_id = concelhos.idconcelhos "; 
$query = mysql_query($sql); 
$array = mysql_fetch_array($query); 
$row = mysql_num_rows($query); 
//$sql_cidades = "SELECT * FROM oferta WHERE localizacao like '$pesquisa_loc'"; 


if (isset($latitude) && isset($longitude)) { 
    if ($row > 0) { 
     do { 
     $endereco=$array['concelho_id']; 
     $request="http://maps.google.com/maps/api/geocode/json?address='.$endereco.'sensor=false"; 
     $json_data=file_get_contents($request); 

     $earth_radius = 6372.795477598; 
     $lat_1 = floatval($latitude); 
     $lon_1 = floatval($longitude); 
     $lat_2 = 41.145570;//floatval($latitude2); 
     $lon_2 = -8.627014;//floatval($longitude2); 
     $delta_lat = $lat_2 - $lat_1 ; 
     $delta_lon = $lon_2 - $lon_1 ; 
     $alpha = $delta_lat/2; 
     $beta  = $delta_lon/2; 
     $a  = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat_1)) * cos(deg2rad($lat_2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ; 
     $c  = asin(min(1, sqrt($a))); 
     $distance = 2*$earth_radius * $c; 
     $distance = round($distance, 4); 
     echo $distance."\n"; 
      if($distance<=$pesquisa_raio){ 
      $response = $array['titulo'] . "|"; 
     } 
    } while ($array = mysql_fetch_assoc($query)); 
     json_encode($response); 

一些錯誤的代碼,或者是我無法理解的結果呢? Sooooooo我有這個代碼不工作,但我解決問題,這是解決方案:

$tipo_output = "json"; // pode ser utilizado o json também 
       // a sua API KEY do Google Maps gerado com o link acima 
       $google_api = "0R4r34bcHA6I0Ppts5oHcxhgoPmdOvt4Hz2cA2w"; 
       // o endereço que desejamos que o google procure 
       // lembrando que o endereço tem que ser no padrão de uma requisição URL e caso possua acentuação, vamos executar um utf8_encode 
       $cidade=$array['concelho']; 
       echo $cidade; 
       $endereco_desejado = urlencode(utf8_encode("$cidade, PT")); 
       // Desired address 
       $endereco_final = "http://maps.google.com/maps/api/geocode/json?address=". $endereco_desejado ."&sensor=true"; 
       // conteudo da página 
       $string = file_get_contents($endereco_final); // get json content 
$json_a = json_decode($string); //json decoder 
      //busca da lat e log 
      $longitude2 = $json_a->results[0]->geometry->location->lng; 
      $latitude2 =$json_a->results[0]->geometry->location->lat; 
      //calculo da distancia 

      $earth_radius = 6371.0; 
      $latitude1 = $latitude * pi()/180.0; 
      $longitude1 = $longitude * pi()/180.0; 
      $latitude2 = $latitude2 * pi()/180.0; 
      $longitude2 = $longitude2 * pi()/180.0; 

      $dLat = $latitude2 - $latitude1; 
      $dLong = $longitude2 - $longitude1; 

      $a = sin($dLat/2) * sin($dLat/2) + cos($latitude1) * cos($latitude2) * sin($dLong/2) * sin($dLong/2); 

      $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); 

      $resultado=intval($earth_radius * $c); // resultado em km. 

TY大家

+0

什麼輸入產生哪些輸出?嘗試計算非常接近您的固定點和離定點更遠的東西。嘗試使用相同緯度和相同經度的值。 – sarnold

回答

0

您的輸入已損壞。

$latitude = floatval("%" . $_GET['lat'] . "%"); 
$longitude = floatval("%" . $_GET['log'] . "%"); 

想想你在做什麼。無論您從$_GET獲取什麼,floatval('%123.4%')都將始終爲零。

這些看起來像來自SQL查詢的剩菜,但是您需要一個不同的轉義方法(例如PDO)來避免SQL注入。


此外,您使用格式錯誤的查詢加載遠程數據,顯然忽略結果?

$endereco=$array['concelho_id']; 
$request="http://maps.google.com/maps/api/geocode/json?address='.$endereco.'sensor=false"; 
$json_data=file_get_contents($request); 

這將產生類似於http://maps.google.com/maps/api/geocode/json?address='.some kind of address.'sensor=false的URL,這是永遠不會給你有用的答案,因爲它是

  1. 包括單引號和週期(你似乎混淆了雙人和單引號) ,
  2. 根本沒有正確轉義動態部分,
  3. sensor=false之前缺少&

(更不用說$array甚至不會運行do {} while循環的第一次初始化)。

+0

TY太多了!但你有任何想法如何正確執行此代碼?因爲我試圖獲取由數據庫提供的城市動態名稱,並且我需要Google地圖提供的經緯度來執行計算,並且我正在搜索並找到這部分代碼: $ endereco = $ array ['concelho_id']; $ request =「http://maps.google.com/maps/api/geocode/json?address='.$endereco.'sensor=false」; $ json_data = file_get_contents($ request); –

+0

TY @DCoder我解決了這個問題:) –

0

我不知道你的代碼,但我會創造一個功能來計算距離。

function getDistance($latitude1, $longitude1, $latitude2, $longitude2) { 
    $earth_radius = 6371; 

    $dLat = deg2rad($latitude2 - $latitude1); 
    $dLon = deg2rad($longitude2 - $longitude1); 

    $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2); 
    $c = 2 * asin(sqrt($a)); 
    $d = $earth_radius * $c; 

    return $d; 
} 

Taken from the codecodex.com page for Haversine formula

我測試的C#式,它是斑上。

+0

ty但這是給我的結果:4657.0761803876 –

+0

我不能不重新設置這個數字,因爲對於這兩個城市之間的距離來說,我把它們放在了一起! 這些是+/- 80km之間的真實距離,所以我不能無視這個結果。 –

+0

ty Chis Stater我已經解決了這個問題:),我會發布解決方案 –