MySQL Great Circle Distance (Haversine formula)

前端 未结 9 1379
栀梦
栀梦 2020-11-21 06:53

I\'ve got a working PHP script that gets Longitude and Latitude values and then inputs them into a MySQL query. I\'d like to make it solely MySQL. Here\'s my current PHP Cod

9条回答
  •  悲&欢浪女
    2020-11-21 07:11

    I thought my javascript implementation would be a good reference to:

    /*
     * Check to see if the second coord is within the precision ( meters )
     * of the first coord and return accordingly
     */
    function checkWithinBound(coord_one, coord_two, precision) {
        var distance = 3959000 * Math.acos( 
            Math.cos( degree_to_radian( coord_two.lat ) ) * 
            Math.cos( degree_to_radian( coord_one.lat ) ) * 
            Math.cos( 
                degree_to_radian( coord_one.lng ) - degree_to_radian( coord_two.lng ) 
            ) +
            Math.sin( degree_to_radian( coord_two.lat ) ) * 
            Math.sin( degree_to_radian( coord_one.lat ) ) 
        );
        return distance <= precision;
    }
    
    /**
     * Get radian from given degree
     */
    function degree_to_radian(degree) {
        return degree * (Math.PI / 180);
    }
    

提交回复
热议问题