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

Over the past few months, we have been working on the Osvita.com.ua project  - 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. Moreover, all the catalogs are made on the principle: here you have 500 gardens with addresses and phone numbers - look. I did not want to make another stupid clone.

I wanted this: enter the address and find all the gardens within the required radius of 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: 1/2 Lunacharsky St., Kyiv ). How did it happen under habrakat

Formulation of the problem

There are large cities, in each of them there are 200-500 gardens with addresses, you need to find the gardens closest to the user's address, say, in an area of ​​1 or 2 km and sort them by distance.

Platform selection

Choose between different providers of free web maps.
• Google Maps : at the time of the decision, Google had not yet launched its maps in Ukraine (now it already exists).
• Yandex.Maps : there are most of the major cities of Ukraine, a good API, good documentation and support, stable high speed access, 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 maps are selected.

Opportunities and challenges

The Yandex.Maps API, like many other systems, offers the following features:
• determination of coordinates by address;
• determination of the distance between two coordinates or addresses;
• display on the map of objects at the given coordinates.

Thus, displaying a specific garden on the map is no problem (very many already do this). To show on the map where the user lives is also easy. Calculating the distance between the garden and the user is again a simple task. But there are not 10 gardens in the city, and making 200-500 queries for each search so that you can find the nearest one is a problem (and it’s easy to go beyond the limit of 25,000 queries a day, and such a search will take a lot of time).

Solution and implementation

We transfer all calculations to the server side. For this:
1. When filling in 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 a user searches for the nearest gardens, again, through the Yandex.Maps API, we determine the coordinates of his address.
3. Further on the server, knowing the coordinates of the user's address, we calculate the coordinates of the area remote from the address by 1 or 2 km.
4. We are looking for all the gardens whose coordinates fell in this area and together with the user’s address we pass their coordinates to the Yandex.Maps API object embedded on the site page.
5. This object draws a map at a given scale and displays all transferred gardens on it. We get the following result: ( test address: 1/2 Lunacharsky St., Kyiv )

How to count

The main chip with coordinates is to convert the radius in kilometers to the radius in degrees.
• one minute of the geographical meridian is 1852 meters.
• in a degree of 60 minutes, i.e. 60 * 1852 meters
• in one kilometer - 1 / (60 * 1.852) degrees
And in order to get the coordinates of the square in which the center will be the starting address,
you need to do a simple addition and subtraction of the radius in degrees.

Epilogue

It seems that in Ukraine we were the first to implement a similar search in kindergartens and schools. I don’t know how this is in Russia, but in a cursory scan, I didn’t see anything like that either. Therefore, I wanted to share. After all, the algorithm is quite simple and will certainly be useful for many projects.