dongzhi4239 2014-07-14 11:52
浏览 76
已采纳

按距离排序邮政编码邻近搜索(php / mysql)

I have a table (user_zip_codes) with the users' zip_code, latitude and longitude. I have found a function here on stackoverflow that finds zip codes within a specific radius:

function zipcodeRadius($lat, $lon, $radius) {
    global $mysqli;
    $zipcodeList = array();
    $radius = ($radius ? $radius : 20);
    $sql = "SELECT userID,city,zip_code,country FROM user_zip_codes  WHERE (3958*3.1415926*sqrt((lat-$lat)*(lat-$lat) + cos(lat/57.29578)*cos($lat/57.29578)*(lon-$lon)*(lon-$lon))/180) <= $radius GROUP BY zip_code";
    if($stmt = $mysqli->prepare($sql)) {
        $stmt->execute();
        $stmt->bind_result($userID,$city,$zip_code,$country);
        while($stmt->fetch()) {
            $zipcodeList[] = array('userID'=>$userID,'city'=>$city,'zip_code'=>$zip_code,'country'=>$country);
        }
    }
    return $zipcodeList;
}

It works perfectly. However, I would like the function to sort the array by distance (either by ASC og DESC). How should I adjust my query in order for this to happen?

Thanks in advance.

UPDATE: The word 'distance' might appear ambiguous (thanks to Jorge). I simply wish to sort the zip_codes by distance meant as the distance between two points.

  • 写回答

3条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 edge跟谷歌浏览器的默认搜索引擎无法修改,如何解决?
      • ¥15 access数据库出错
      • ¥100 matlab数学建模习题,求解答
      • ¥15 blender python输出纹理坐标(像素级)与三维坐标的对应关系
      • ¥15 python文件分发
      • ¥15 关于#c语言#的问题:要求:输出格式要界面直观、清晰大方、格式规范
      • ¥15 计算机组成汇编语言电路升级
      • ¥15 hadoop网站点击流数据分析项目
      • ¥15 TDA4 USB 识别不到的问题
      • ¥36 禁用ssl重协商以达到解决DOS漏洞