0

I am being sent the user's latitude and longitude from an app. And I need to show the stores that are 2 km away from those coordinates from the database. It doesn't feel right to me to bring all my data and then calculate. Is there a way that I can fetch not all my data from the database, but only those that match this 2 km condition.

Elchin
  • 39
  • 6
  • Based on your tags, you are using MySQL. It has support for spatial data types: https://dev.mysql.com/doc/refman/8.0/en/spatial-type-overview.html. You should be able to store the locations in a geographical column and then query it with something like ST_Distance() < 2000: https://dev.mysql.com/doc/refman/8.0/en/spatial-relation-functions-object-shapes.html – juunas Feb 20 '23 at 08:21
  • Answers to the duplicate question show both the mathematical way of calculating distance in mysql and also the use of spatial functions. – Shadow Feb 20 '23 at 08:26
  • If you want a really simple method that works with any database server, you can retrieve only those locations whose latitude and longitude are each within plus or minus 2 km. Some of those locations will be more than 2km away, but you would be eliminating any location that couldn't possibly be within 2 km. – pjs Feb 20 '23 at 12:25
  • 1
    5 algorithms: [_Find Nearest_](http://mysql.rjweb.org/doc.php/find_nearest_in_mysql) They rank from terrible performance to wonderful. – Rick James Feb 21 '23 at 02:29

0 Answers0