3

I know that this question is duplicate, but i had a problem finding the best solution for the specific use of Firebase Firestore as a database.

Here is the problem:

I have a lot of markers in my Firestore Database and I would like to load only markers near me.

One solution could be load all markers and set visibility to false if they are out the LatLngBounds, but this is not the case.

Why?

You will always request all markers from your database and this is very useless specially if you have a tons of markers and you only need to load a few of them.

I asked this question to help users deal with this problem. Below you can find the solution.

Matteo Antolini
  • 2,560
  • 2
  • 19
  • 30

1 Answers1

3

HERE IS THE SOLUTION THAT I USED

I made a query on my Firestore Database and create a Box around my position.

FirebaseFirestore database = FirebaseFirestore.getInstance();

CollectionReference locals = database.collection("locals");
//I created a box with user LatLng. Every marker inside this box will be loaded
Query markers = locals.whereGreaterThanOrEqualTo("location", new GeoPoint(userLat - 0.5, userLng - 0.5))
.whereLessThanOrEqualTo("location", new GeoPoint(userLat + 0.5, userLng + 0.5));

    markers.get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()){ 

                            String localName = (String) document.get("localName");
                            GeoPoint location = (GeoPoint) document.get("location");
                            LatLng localPosition = new LatLng(location.getLatitude(), location.getLongitude());
                            Marker marker = mMap.addMarker(new MarkerOptions()
                                    .position(localPosition)
                                    .title(localName)
                                    );
                        }
                    }
                }
            });

Here is an example: enter image description here

Your request will load only markers inside this box.

Why this is very important?

If you have 5000 markers in your database and you have 10.000 daily users, every time a user opens the map he will request 5000 documents to your database. If 10.000 people open the map once a day there will be 50.000.000 requests to documents. If you have a Blaze plan this will cost you $29.10 daily.

Try to imagine if you have 100.000 users... there will be $299.10 daily.

With my solution you load only documents in a specific area that you can define increasing or decreasing the box.

Hope I helped many users. if someone has a better solution, I'm ready to listen.

Matteo Antolini
  • 2,560
  • 2
  • 19
  • 30
  • 1
    Hi Matteo - this is awesome! Can I ask you, how do you implement it without the box? If I just want all the markers. I dont have a userLat and userLng, which is giving me the problems. In my collection, I have a document with a GeoPoint containing two numbers/coordinates.. – nelion Dec 08 '19 at 16:23
  • @badaboomskey replace `markers.get()` to `locals.get()` – Matteo Antolini Dec 09 '19 at 10:34