dongliao6777 2015-07-07 00:13
浏览 57
已采纳

根据纬度和经度获取半径范围内的拉链码

I am trying to get zipcodes within a radius of 160km of the provided latitude/longitude coordinates. I already have a database of zipcodes, latitudes & longitudes, my problem is calculate the radius.

I have been looking at examples of how to do this, and this is what I came up with. But evidently something is wrong, because I'm getting distances that are ~9000km away according to the $dist variable.

public function getClose($latitude, $longitude) {
    $latitudes  = [floor($latitude)-2, ceil($latitude)+2];
    $locations  = Zipcode::whereBetween('latitude', $latitudes)->get();

    foreach ($locations as $location) {
        $venueLat = $location->latitude;
        $venueLng = $location->longititude;

        $latDistance = deg2rad($latitude - $venueLat);
        $lngDistance = deg2rad($longitude - $venueLng);
        $a = (sin($latDistance / 2) * sin($latDistance / 2)) + (cos(deg2rad($latitude))) * (cos(deg2rad($venueLat))) * (sin($lngDistance / 2)) * (sin($lngDistance / 2));
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

        // Distance between 2 points in km. 6371 = Earths radius
        $dist = 6371 * $c;
        var_dump($dist); // This is the line saying it's about 9000km away

        if ($dist < 160){
            var_dump($location->zip);
        }
    }
}

That code was mostly based on this answer on a related question: https://stackoverflow.com/a/18465217

But, obviously something didn't translate right, or I misunderstood something. Any help would be vastly appreciated!

  • 写回答

1条回答 默认 最新

  • dongya8378 2015-07-08 16:32
    关注

    Here is the code that I ended up using that works. A couple things changed, but the biggest difference is the formula. As you can see they're significantly different. I'm not very good at math, so I don't really know what the differences are, but I do know this one works.

    public function getClose($latitude, $longitude) {
        $latitudes  = [floor($latitude)-2, ceil($latitude)+2];
        $longitudes = [floor($longitude)-1, ceil($longitude)+1];
        $locations  = Zipcode::whereBetween('latitude', $latitudes)->whereBetween('longitude', $longitudes)->get();
        $zips       = [];
    
        foreach ($locations as $location) {
            $theta = $longitude - $location->longitude;
            $dist = sin(deg2rad($latitude)) * sin(deg2rad($location->latitude)) +  cos(deg2rad($latitude)) * cos(deg2rad($location->latitude)) * cos(deg2rad($theta));
            $dist = acos($dist);
            $dist = rad2deg($dist);
            $miles = $dist * 60 * 1.1515;
    
            if ($miles <= 100){
                $zips[] = $location->zip;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题