📜 ⬆️ ⬇️

We make the search for kindergartens and schools really convenient. Applied use of Yandex Maps

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 ).
')
image

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.

Result: Yandex cards are selected.

Opportunities and challenges


The Yandex Maps API, like many other systems, offers the following features:

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:
  1. 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.
  2. When searching for the nearest gardens by the user, again, through the Yandex-Maps API, we determine the coordinates of its address.
  3. 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.
  4. 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.
  5. 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.
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.

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


All Articles