3

I have these data:

latitude: 50.223137

longitude: 18.679558

and now I would like to create a special function in PHP which will be able to calculate minimum lat & long and maximum lat & long which are nearer than 50 km. Finally I would like to get 4 parameters and I would like to use these parameters in simple SQL query: SELECT * FROM table WHERE lat >= [min_lat] AND lng >= [min_lng] AND lat <= [max_lat] AND lng <= [max_lng];

I know, I can do it using only one sql query: latitude/longitude find nearest latitude/longitude - complex sql or complex calculation but I have 20.000.000 records in my database now and it still grow up. I also have to search in my sql database other things so I want calculate everything based on PHP and then search in sql in that way: SELECT * FROM table WHERE lat >= [min_lat] AND lng >= [min_lng] AND lat <= [max_lat] AND lng <= [max_lng]; because I think - this is faster way.

I also found this function: https://www.geodatasource.com/developers/php and this is great but I want create that function in other way - I want declare as argument: lat, lng and distance (for example: 50 km). finally I would like to get 4 parameters - min_lat, min_lng, max_lat, max_lng.

Anybody can help me to create this function?

Thanks.

Community
  • 1
  • 1
Majkson
  • 227
  • 4
  • 15
  • Do you need a precise 50km radius from your position? – ahPo Jun 01 '16 at 21:03
  • That's example. It can be 50km, 20km, 10km, 80km etc. I want to declare that parameter (and lat & long about start point). – Majkson Jun 01 '16 at 21:04
  • You know that 1 degree is around 55km at 99% precision so you'd add and subtract the degrees resulting by dividing your kms by 55 to get your max and min latitudes and longitudes from your postion – ahPo Jun 01 '16 at 21:16
  • 55 km ?? Here @Jim Lewis said, that's 110,5 km: http://stackoverflow.com/questions/1253499/simple-calculations-for-working-with-lat-lon-km-distance/1253545#1253545 – Majkson Jun 01 '16 at 21:17
  • You're right 110km is around 1 degre sorry.... – ahPo Jun 01 '16 at 21:18
  • OK, latitude I can calculate, but I have a problem with longitude. Look here: http://pastebin.com/nzbWUPZx - if I want to get longitude which will near 50km, I have to add/substract 100.13169160071 ?? It's too much for me. latitude is ok - if i add/substract 0.45218586647856, I think it's 50 km from start point. Where is problem with that longitude ? – Majkson Jun 01 '16 at 21:26

1 Answers1

9

You can use this code ang give a try. Do some benchmarks.

<?php

$lat = 50.223137; //latitude
$lon = 18.679558; //longitude
$distance = 50; //your distance in KM
$R = 6371; //constant earth radius. You can add precision here if you wish

$maxLat = $lat + rad2deg($distance/$R);
$minLat = $lat - rad2deg($distance/$R);
$maxLon = $lon + rad2deg(asin($distance/$R) / cos(deg2rad($lat)));
$minLon = $lon - rad2deg(asin($distance/$R) / cos(deg2rad($lat)));

echo $maxLat, "<br>", $minLat, "<br>", $maxLon, "<br>", $minLon;

Output:

50.672797802959
49.773476197041
19.382380506501
17.976735493499
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • The solution is great. There is one note. If the `$distance` is greater then `$R` you'll get `NaN` as of minLng and maxLng that cause to fail in future usage. You can fix that with a simple if statement before going into the calc: `$distance = $distance >= $R ? $R : $distance` – Gkiokan Oct 02 '20 at 10:56