Nearby Places Search Approach Using Firebase Data

Hi Thunkers!

After some research, I realize that using Google Maps Places API will be VERY expensive when you have users searching for POIs (Points Of Interest) massively.

Fortunately, I found the information about the coordinates of all POI that I want to show in my project (and how to keep it updated). This way, instead “google it”, I can save the data in my own Firebase DB (or Airtable, if is faster).

The mission now, is to find the nearby POIs FASTER as possible in my database. For each POI I have the place name, exact latitude and longitude, plus some extra info.

My first idea was to store the POIs under the combination of the LATITUDE and LONGITUDE disregarding the precision, according my needs. To get an ~ 10km² area, is just save only 1 decimal in the coordinates for LAT & LON. So my database structure will look like this:

  MyFirebaseDB
      +-- Latitude_With_ 1_Decimal + Longitude_With_1_Decimal 
          +-- Place_Name
              +-- Precise_Latitude
              +-- Precise_Longitude

So, I can easily find all the places inside the “square” of user’s current area,

For example:

Current user Location: -30.03123443 Lat & -51.23054732 Lon (got from user location block)

Then I search in Firebase the key “-30.0±51.2”. If something is found, means that there are at least 1 POI inside the current user square area. Now it’s just a matter to read the exact coordinates of each POI and plot in the map.

The problem with this approach is that if the user are near from the “border” of the square area, you will miss the all the POIs that are near in the adjacent areas. Look the figure below to understand the situation:

So I need to improve my algorithm to read the POIs of the 8 (yes, 8!) adjacent areas too, calculating the distance of the existing POIs in these areas (using the haversine formula), and disregard all that are higher of 10 km. This way I can have the POIs that are really at 10km distance maximum.

That’s the way I found to solve this challenge.

My question is if somebody knows any other approach that can be FASTER than this, considering the fact that I don’t want to read from Google Maps to save some $$$, or this is the best way to do it!

Thanks in advance!

Regards,

Paulo Vaz

2 Likes