Filter wordpress posts by using a bounding box on the lat and lon

Regarding the answer given by enigma here https://stackoverflow.com/a/29753454/4804502

This is almost perfect for what I need to achieve, as using WP_Query, I can add further meta data to filter the returned posts, but have come up with a problem returning the coords for the bounding box.

Read More

So need to filter a search for other posts within say a 2 km radius.
Using the provided functions by enigma, I generate a bounding box to enter two different meta queries.

But, although the longitude is returned correctly, slightly higher than original longitude, the latitude is returned nowhere near and can not for the life of me figure out why.

This is original latitude and longitude..

13.736979
-100.55612300000007

This is returned from the bounding box.:

Array
(
    [0] => 1.17060885652
    [1] => 1.17060791476
    [2] => 100.556121791
    [3] => 100.556124209
)

I’m guessing its the following function,

function get_destination_lat($lat1, $lng1, $d, $brng){
    return asin( sin($lat1)*cos($d/$R) +
                    cos($lat1)*sin($d/$R)*cos($brng) );
}

and also guessing that there should be a $lat + in between return and asin, this at least gives it a somewhere closer latitude, but not less than or greater than.

Could really do with a little help from someone who can review the following and advise on a solution.

PS.. this is the three functions in question..

function get_destination_lat($lat1, $lng1, $d, $brng){
    return asin( sin($lat1)*cos($d/$R) +
                    cos($lat1)*sin($d/$R)*cos($brng) );
}

function get_destination_lng($lat1, $lng1, $d, $brng){
    $lat2 = get_destination_lat($lat1, $lng1, $d, $brng);
    return $lng1 + atan2(sin($brng)*sin($d/$R)*cos($lat1),
                         cos($d/$R)-sin($lat1)*sin($lat2));
}

function get_bounding_box($lat, $lng, $range){
    // latlng in radians, range in m
    $latmin = get_destination_lat($lat, $lng, $range, 0);
    $latmax = get_destination_lat($lat, $lng, $range, deg2rad(180));
    $lngmax = get_destination_lng($lat, $lng, $range, deg2rad(90));
    $lngmin = get_destination_lng($lat, $lng, $range, deg2rad(270));
    // return approx bounding latlng in radians
    return array($latmin, $latmax, $lngmin, $lngmax);
}

Also, this is the output of the $args array:

[query] => Array
    (
        [posts_per_page] => 24
        [category] => 
        [orderby] => post_date
        [order] => DESC
        [post_type] => dt_agents
        [post_status] => publish
        [meta_query] => Array
            (
                [relation] => AND
                [0] => Array
                    (
                        [key] => _building_latitude
                        [value] => Array
                            (
                                [0] => 1.17060885652
                                [1] => 1.17060791476
                            )

                        [type] => numeric
                        [compare] => BETWEEN
                    )

                [1] => Array
                    (
                        [key] => _building_longitude
                        [value] => Array
                            (
                                [0] => 100.556121791
                                [1] => 100.556124209
                            )

                        [type] => numeric
                        [compare] => BETWEEN
                    )

            )

    )

Many thanks in advance..

Related posts