Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: finding latitude and longitude boundaries based on a central lat/lng and distance

I was working with the solution to a very similar question and upon implementation I discovered that it was producing a tall rectangle with the returned coordinates instead of a square (please see Matthias' answer to the other question).

I only needed an array to be returned as this is to work with WordPress which has it's own preferred query method.

Here's my implementation:

function bar_get_nearby( $lat, $lng, $limit = 50, $distance = 50, $unit = 'mi' ) {
    
    // radius of earth; @note: the earth is not perfectly spherical, but this is considered the 'mean radius'
    if( $unit == 'km' ) { $radius = 6371.009; }
    elseif ( $unit == 'mi' ) { $radius = 3958.761; }

    // latitude boundaries
    $maxLat = ( float ) $lat + rad2deg( $distance / $radius );
    $minLat = ( float ) $lat - rad2deg( $distance / $radius );

    // longitude boundaries (longitude gets smaller when latitude increases)
    $maxLng = ( float ) $lng + rad2deg( $distance / $radius / cos( deg2rad( ( float ) $lat ) ) );
    $minLng = ( float ) $lng - rad2deg( $distance / $radius / cos( deg2rad( ( float ) $lat ) ) );

    $max_min_values = array(
        'max_latitude' => $maxLat,
        'min_latitude' => $minLat,
        'max_longitude' => $maxLng,
        'min_longitude' => $minLng
    );
    
    return $max_min_values;
    
}

If I give I geocode (via Google Maps API) my desired postcode of G2 1QX and a distance of 5 miles I get a lat/lng of -4.2556347/55.8620472 with the function returning this array:

Array
(
    [max_latitude] => -4.18326890233
    [min_latitude] => -4.32800049767
    [max_longitude] => 55.9346130696
    [min_longitude] => 55.7894813304
)

Any ideas? Many thanks in advance.

Cheers, RS

like image 273
RS- Avatar asked Sep 14 '12 12:09

RS-


1 Answers

Here is your function with my Modifications in it that will give you a square coordinates instead of tall rectangle:

function bar_get_nearby( $lat, $lng, $limit = 50, $distance = 50, $unit = 'mi' ) {
    // radius of earth; @note: the earth is not perfectly spherical, but this is considered the 'mean radius'
    if( $unit == 'km' ) { $radius = 6371.009; }
    elseif ( $unit == 'mi' ) { $radius = 3958.761; }

    // latitude boundaries
    $maxLat = ( float ) $lat + rad2deg( $distance / $radius );
    $minLat = ( float ) $lat - rad2deg( $distance / $radius );

    // longitude boundaries (longitude gets smaller when latitude increases)
    $maxLng = ( float ) $lng + rad2deg( $distance / $radius) / cos( deg2rad( ( float ) $lat ) );
    $minLng = ( float ) $lng - rad2deg( $distance / $radius) / cos( deg2rad( ( float ) $lat ) );

    $max_min_values = array(
        'max_latitude' => $maxLat,
        'min_latitude' => $minLat,
        'max_longitude' => $maxLng,
        'min_longitude' => $minLng
    );

    return $max_min_values;
}

Cheers,

Rupesh Kamble

like image 54
Rupesh K Avatar answered Nov 15 '22 16:11

Rupesh K