-1

I have two variables which define a static longitude and latitude. I want to be able to define a multi dimensional array which holds the following: id, longitude and latitude.

What i want to achieve - from the static longitude to feed through the loop of arrays and if finds something within a radius of 50 miles (or whatever), it selects the potential ones and lists them.

I am not sure what formula or algorithm i can use to check the radius and bring back the nearest values.

Please note: not using databases in this example.

So far:

$mainLong = 57.7394571;
$mainLat = -4.386997;

$main = array(
        array("loc_1",57.7394571,-4.686997),
        array("loc_2",51.5286416,-0.1015987),
        array("loc_3",51.2715146,-0.3953564),
        array("loc_4",50.837418,-0.1061897)
);

foreach ( $main as $key => $value ) {

    if($value[1] == $mainLong){
      print_r($value);
    }

}
jagmitg
  • 4,236
  • 7
  • 25
  • 59
  • The haversine formula is one option [related question](http://stackoverflow.com/questions/14750275/haversine-formula-with-php) – geocodezip Nov 13 '14 at 22:42

1 Answers1

0

Haversine formula could help. Here is a sample algorithm. You can try it out.

%% Begin calculation

R = 6371;                                   % Earth's radius in km
delta_lat = locs{2}(1) - locs{1}(1);        % difference in latitude
delta_lon = locs{2}(2) - locs{1}(2);        % difference in longitude
a = sin(delta_lat/2)^2 + cos(locs{1}(1)) * cos(locs{2}(1)) * ...
    sin(delta_lon/2)^2;
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;                                 % distance in km


%% Convert result to nautical miles and miles

nmi = km * 0.539956803;                     % nautical miles
mi = km * 0.621371192;                      % miles