Search for objects in a certain range of a given coordinate

I have a django based web application that stores locations.

I have an Android mobile app that pulls locations from a web application and saves locations in a web application. Places returned to me are loaded into mapoverlay in my application.

I would like to send my current coordinates and return a list of places that are in a certain range. For example, I send my location and return items within a radius of 2 km. (Similar to how the Google Places API works, just by browsing my data).

It is probably best to send location coordinates and execute all this logic in my python django application. Then return the list of correct locations and just show the places on my map.

I don’t know where to start doing this. How can I filter my saved locations within a specific radius (km) based on a given set of coordinates?

+4
source share
4 answers

The Haversin equation is the answer to your question. However, this is a bit difficult to decipher, so here I provide you with a simple explanation:

Simply put:

Here's an example / example of an SQL statement that will find the next 20 locations within a radius of 25 miles from coordinate 37, -122. It calculates the distance based on the latitude / longitude of this line and the target latitude / longitude (given by lat / lng in the equation below), and then it only asks for lines where the distance value is less than 25, orders the entire request by distance, and limits it to 20 results. To search by kilometers instead of miles, replace 3959 with 6371.

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20; 

You can convert sql to whatever you want. I mean, the principle remains the same.

+11
source

The easiest approach is to calculate the distance to each place and select points at a specific distance. If you want to perform searches faster, you can arrange your locations in a more complex data structure (such as kd-tree ).

+1
source

GeoDjango integrates with the excellent PostGIS add on for Postgres, which provides you all these remote requests for free.

If you have a LatLong stored in a model that is stored as a Postgres type, you can run a trivial ORM query to get all locations at a certain distance from the current LatLong.

GeoDjango is very powerful and has many options, and if all you have to do is just find the locations from the list at a given distance, you can simply use simple math: distance = sqrt (dx ^ 2 + dy ^ 2)

+1
source

I am currently working on this feature in an Android application, I came across this, hope this helps. At first I was going to filter out the JSON result from the server, then I came to this and realized that Mysql is much more complicated than I initially perceived it.

https://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

The comment above did not work for me, but they are on the money, going with the Haversin Equation. It is much more efficient to do most of this on the server side, so the phone does not receive suppression of information that will not be used for anything. It's like baking a fully blown 10-inch cake, just to slice the eighth and throw the rest away, why not just bake a tiny pie with a radius of 2 inches? This may not seem like very different, but like any good program, get only what you need if you don't need all of this.

to use the live working version, run a small test using your own code, use this link below. http://sqlfiddle.com/#!2/abba1/2

Hope this post helps.

0
source

Source: https://habr.com/ru/post/1416116/


All Articles