duanchu7271 2014-05-14 15:52
浏览 111
已采纳

按距离排序邮政编码

So, I'm working on a search form for my site. I've run into a road block. Seems others have too but I've been unable to find an answer for my question, at least one that I understand.

I found a PHP function online that grabs up all the zip codes within a specified radius. I edited the sql query so that I wasn't grabbing every zip code within that radius, but ones that are associated with a user account. The function works and grabs the zip codes I'm after. This function returns an array with the zip codes. I would like to sort the zip codes by distance from nearest to farthest, and apparently I'm no where near smart enough to figure out how to do it.

What I've Tried:

I found another PHP function that will calculate the distance between 2 sets of coordinates(lat,lon2, lat2, lon2). I created my own function that loops through my zip codes array and runs each zip through the calculateDistance function. Just seems to be a big mess. I believe I have all the pieces just not sure how to put it all together. Any help would be greatly appreciated.

Here is the zipcodeRadius function I'm using:

    // get all the zipcodes within the specified radius - default 20
function zipcodeRadius($lat, $lon, $radius)
{
$radius = $radius ? $radius : 20;
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        $sql = 'SELECT DISTINCT zipcodes.ZIP,
                     members.*,
                     location.*
                     FROM members
                     LEFT JOIN location
                     ON members.id = location.user_id
                     LEFT JOIN zipcodes
                     ON location.zip = zipcodes.ZIP
                     WHERE activated="1"
                     AND (3958*3.1415926*sqrt((Latitude-'.$lat.')*(Latitude-'.$lat.') + cos(Latitude/57.29578)*cos('.$lat.'/57.29578)*(Longitude-'.$lon.')*(Longitude-'.$lon.'))/180) <= '.$radius.';';
                     $result = mysqli_query($dbc, $sql);
// get each result
$zipcodeList = array();
while($row = mysqli_fetch_array($result))
{
    array_push($zipcodeList, $row['ZIP']);
}
return $zipcodeList;
}

Here is the calculateDistance function

    function calculateDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
return $miles;
}   

And, Here's my seemingly useless function:

    $matches = join(',',$zipCodeArray);
function sort_zip_by_distance($matches, $lat, $lon){
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$queryDistance = "SELECT * FROM zipcodes WHERE  ZIP IN ($matches)";
$resultDistance = mysqli_query($dbc, $queryDistance)
or die("Unable to query the database");
while($rowDistance = mysqli_fetch_array($resultDistance)){
    $miles = calculateDistance($lat, $lon, $rowDistance['Latitude'],        $rowDistance['Longitude']);
    echo '<p>ZIP: ' . $rowDistance['ZIP'] . ' | Lat: ' . $rowDistance['Latitude'] . ' | Lon: ' . $rowDistance['Longitude'] . ' | Miles: ' . $miles . '</p><br />';

    $distanceTotal = array();
    array_push($distanceTotal, $rowDistance['ZIP'], $miles);
}
return $distanceTotal;
}
  • 写回答

1条回答 默认 最新

  • dowdw44426 2014-05-14 15:55
    关注

    You can do arbitrary math in order by clauses, but you'd most likely to actually RETURN the distances found, so why not do

    SELECT *, long_ugly_distance_calc_here AS distance
    ...
    ORDER BY distance DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据