The last few months we have been working on the project
Osvita.com.ua - education in Ukraine. Among other sections, 2 were devoted to preschool and school education - “Schools” and “Kindergartens”, the choice of which for the majority is determined by the territorial location - the closer to home, the better. In this case, all the directories are made according to the principle: here you have 500 gardens with addresses and phone numbers - look. I didn’t want to do another stupid clone.
I wanted this: enter the address and find all the gardens in the desired radius from the house, and even sorted by distance, and even see on the map.
And everything turned out to be quite real. Here's what happened: (
test address: Kiev, Lunacharsky St., 1/2 ).
')

How did it happen under habrakat
Formulation of the problem
There are large cities, in them 200-500 gardens with addresses, it is necessary to find the gardens nearest to the user's residential address, say, in the region of 1 or 2 km and sort them by distance.
Platform selection
Chose between different providers of free web maps.
- Google Maps : at the time of the decision, Google has not yet launched its maps in Ukraine (now it already exists).
- Yandex maps : there are most of the major cities of Ukraine, good API, good documentation and support, stable high access speed, sometimes problems with Ukrainian street names.
- Other cards ( Visicom , Mapia , etc.): different problems depending on the developer - less convenient api, lack of examples and documentation, poor implementation of maps, poor speed.
Result: Yandex cards are selected.
Opportunities and challenges
The Yandex Maps API, like many other systems, offers the following features:
- determination of coordinates by address;
- determining the distance between two coordinates or addresses;
- output of objects on the map according to given coordinates.
Thus, it is no problem to display a specific garden on the map (many people already do this). It is also easy to show on the map where the user lives. Calculate the distance between the garden and the user is again a simple task. However, there are not 10 gardens in the city, but making 200-500 requests for each search, then finding the closest one is already a problem (and it’s easy to get out of the limit of 25,000 requests per day, and such a search will take quite a bit of time).
Solution and implementation
We transfer all calculations to the server side. For this:
- When filling the database of kindergarten addresses, we make 1 request to the Yandex Maps API, determine the coordinates of this address and enter it into the database along with the address.
- When searching for the nearest gardens by the user, again, through the Yandex-Maps API, we determine the coordinates of its address.
- Next on the server, knowing the coordinates of the user's address, we calculate the coordinates of the area, which is 1 or 2 km away from the address.
- We are looking for all the gardens, the coordinates of which fell into this area and, together with the user's address, pass their coordinates to the Yandex-Maps API object embedded on the site's page.
- This object draws a map at a given scale and displays all transferred gardens on it. We get this result: ( test address: Kiev, ul. Lunacharsky, 1/2 )
How to count
The main feature with coordinates is to convert the radius in kilometers into radius in degrees.
- one minute of a geographical meridian is equal to 1852 meters.
- 60 degrees, i.e. 60 * 1852 meters
- in one kilometer - 1 / (60 * 1.852) degrees
And to get the coordinates of the square in which the center will be the source address,
it is necessary to make a simple addition and subtraction of the radius value in degrees.
Epilogue
It seems that in Ukraine we were the first to implement a similar search in kindergartens and schools. I do not know how this is in Russia, but with a cursory review nothing of the kind was encountered either. Therefore, I wanted to share. After all, the algorithm is quite simple and will certainly be useful for many projects.