【问题标题】:PHP distance radius searchPHP距离半径搜索
【发布时间】:2014-07-16 05:29:15
【问题描述】:

我一直在网上搜索以查找允许用户输入邮政编码和特定距离并执行搜索的代码。例如(邮编)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、AutoTrader 以及其他一些网站上找到。

【问题讨论】:

  • 整个mysql_ 系列函数已经被弃用了一段时间。您应该在您生成的任何新代码中使用 MySQLi 或 PDO。
  • 为此使用谷歌地图;) 进行 API 调用并将结果存储在数据库中,例如(ID、ZIP_From、ZIP_To、距离)。 mysql_* 被标记为已弃用
  • @meagar 谢谢我知道我需要将我的代码从 mysql 更新到 mysqli 等。我刚开始使用 php,我使用的书是一本旧书并使用了 mysql 语句。
  • @adrian preuB 我以前没有使用过 google maps api,所以现在我需要看看它涉及到什么以及它是否容易。

标签: php distance postal-code


【解决方案1】:

你在找这个吗:http://database.appserver.aeglobalresearch.com/radius.php?lat=51.00000000&lng=5.86666700&dist=10

我构建了这个,它在 gps 位置周围 10 公里半径内搜索街道名称:51.00000000&lng=5.86666700

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多