dro80463 2013-01-11 04:07
浏览 23
已采纳

PHP和MySQL地理距离计算

I have a working PHP function that takes in latitude/longitude coordinates and a distance in miles, then tests row by row in a MySQL table of zip codes/lat-lon pairs (40,000+ rows) whether each zip code/lat-lon row falls within the specified distance or not.

I know that this method is terribly inefficient and I would like to do all of the trigonometry and distance calculation in the SQL query rather than with PHP. Can anyone help me out here? I feel like I've tried everything but I just can't get the SQL query correct. Any help would be greatly appreciated!

Existing PHP function:

function getZips($lat, $lon, $radius)
{   

//MySQL bit
$zipQuery = mysql_query("SELECT * FROM zip_codes");
while ($row = mysql_fetch_assoc($zipQuery))
{

    $lat2 = $row['latitude'];
    $lon2 = $row['longitude'];

    $distance = (3958*3.1415926*sqrt(($lat2-$lat)*($lat2-$lat) + cos($lat2/57.29578)*cos($lat/57.29578)*($lon2-$lon)*($lon2-$lon))/180);

    if ($distance <= $radius)
    {
        $zip = $row['zip'];
        echo "Zip in range: <b>" .$zip . "</b><br>";
    }

}

}
  • 写回答

2条回答 默认 最新

  • douduonang3169 2013-01-11 04:10
    关注
    SELECT zipcode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM logic_zipcodes HAVING distance <= {$radius} ORDER BY distance
    

    can't remember where I got this originaly, but I have been using it for a long time. Here is my function:

    function zipsearch($start, $rad) {
    // ITITIAL POINT
    $result = mysql_query("SELECT latitude,longitude FROM logic_zipcodes WHERE zipcode='" . $start . "'");
    while($row = mysql_fetch_array($result)) {
        $coords = array('latitude' => $row['latitude'], 'longitude' => $row['longitude']);
    }
    //RADIUS
    $radius = $rad;
    // SQL FOR MILES
    $sql = "SELECT zipcode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM logic_zipcodes HAVING distance <= {$radius} ORDER BY distance";
    
    // OUTPUT THE ZIPCODES AND DISTANCES
    $query = mysql_query($sql);
    $zips = array();
    while($row = mysql_fetch_assoc($query)) {
        $zips[] = $row['zipcode'];
    }
    return $zips;
    }
     function getZips($zip, $rad) {
    $zipcode = qt($zip);
    $andstatement = implode("','", zipsearch($zipcode, $rad));
    return "AND zipcode in('" . $andstatement . "')";
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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 海浪数据 南海地区海况数据,波浪数据