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 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码