PHP距离半径搜索

时间:2014-03-09 17:22:32

标签: php distance postal-code

我一直在网上搜索能够让用户输入邮政编码和特定距离并执行搜索的代码。例如(邮编)dd1(下拉距离可以在10,50,100,200英里之内)50英里。

我已经有了代码,您可以输入2个邮政编码来计算距离,并考虑修改但不成功。代码是UK postcodes.org

<?php
$dbName="";
$dbUsername="";
$dbPassword="";

$fromPC=$_POST['fromPC'];
$toPC=$_POST['toPC'];

function getDistance($lat1, $long1, $lat2, $long2, $unit)
{
  if($unit=="miles"){
      $earth = 3960; //miles
  }else{
     $earth = 6371; //kilometres
  }

    //From co-ordinates
    $lat1 = deg2rad($lat1);
    $long1= deg2rad($long1);

    //To co-ordinates
    $lat2 = deg2rad($lat2);
    $long2= deg2rad($long2);

    // The Haversine Formula
    $dlong=$long2-$long1;
    $dlat=$lat2-$lat1;

    $sinlat=sin($dlat/2);
    $sinlong=sin($dlong/2);

    $a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);

    $c=2*asin(min(1,sqrt($a)));

    $d=round($earth*$c);

    return $d;
}

if( (!empty($fromPC)) && (!empty($toPC)) )
{
    mysql_connect('localhost','username','password');
    @mysql_select_db('dbname') or die( "Unable to select database");

  // basic cleaning of input
    $firstPC = strtoupper(preg_replace("/[^a-zA-Z0-9]/","",$fromPC ));
    $secondPC = strtoupper(preg_replace("/[^a-zA-Z0-9]/","",$toPC ));

    // get first details
    $query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `postcode`="'.$firstPC.'";';
    $result = mysql_query($query);
    $first = mysql_fetch_row($result);
    $checkFirst=mysql_num_rows($result);

  // get second details
    $query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `postcode`="'.$secondPC.'";';
    $result = mysql_query($query);
    $second = mysql_fetch_row($result);
  $checkSecond=mysql_num_rows($result);

  // ensure there were results to calculate with
  if( ($checkFirst<1) || ($checkSecond<1) ){
      $outputResults="Unrecognised postcode entered.";
  }else{
        $distance = getDistance($first[0], $first[1], $second[0], $second[1], "miles");
        $outputResults = "The distance between postcode: $firstPC and postcode: $secondPC is ".$distance." miles.";
  }

  // always close your connections !!
    mysql_close();
}

?>
<h1>UKPostcodes.org - distance calculator</h1>
<form action="uk_postcodes.php" method="post">
  Please enter the first part of the postcodes only:<br/><br/>
  From postcode: <input maxlength="4" name="fromPC"/><br/>
  To postcode: <input maxlength="4" name="toPC"/><br/>
  <input type="submit" />
</form>

<?php echo $outputResults?> 

我所追求的例子可以在postcode-distance.com,自动交易者以及其他一些网站上找到。

1 个答案:

答案 0 :(得分:0)

您是否在寻找:http://database.appserver.aeglobalresearch.com/radius.php?lat=51.00000000&lng=5.86666700&dist=10

我构建它,它搜索半径为10公里的街道名称,围绕gps位置:51.00000000&amp; lng = 5.86666700

如果是,我可以给你完整的radius.php文件,你可以修改它。 我也进行了大量的搜索以使其正常工作:)