Home > Software design >  Google Maps and Firebase - What data type should I use for Storing markers by City?
Google Maps and Firebase - What data type should I use for Storing markers by City?

Time:01-21

When a user opens the map, I want to display all the markers in their city.

What data type should I use as the key for children of the Markers node?

I thought about using zip codes, but after googling, not all places around the world use zipcodes like in the USA.

I also thought about using a city name like Chicago, but what if there's another city with the same name?

Does anyone have any suggestions on what I should use?

Firebase RTDB

CodePudding user response:

If you want to display all markers from the Realtime Database that correspond to a certain city, I recommend you a schema that looks like this:

db
|
--- markers
      |
      --- USA (countryName)
           |
           --- Illinois (stateName)
                |
                 --- Chicago (cityName)
                      |
                      --- $markerId
                            |
                            --- zipCode: "123456"
                            |
                            --- name: "markerOne"
                            |
                            --- dateCreated: 1674178220
                            |
                            --- lat: 31.12345
                            |
                            --- lng: -31.12345

Since I never heard about two cities that share the same within the same state, the above schema will do the trick. The zip code is not so important, because as you said, there are countries that do not have zip codes.

To query such a structure, the following DatabaseRefrence is required:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference cityNameRef = db.child("markers") 
                                  .child(countryName)
                                  .child(stateName)
                                  .child(cityName);
cityNameRef.get().addOnCompleteListener(/* ... /*);

On the other hand, when using Firestore, to achieve the exact same result, the following schema will do the trick:

db
|
--- markers (collection)
     |
     --- $markerId (document)
           |
           --- countryName: "USA"
           |
           --- stateName: "Illinois"
           |
           --- cityName: "Chicago"
           |
           --- zipCode: "123456"
           |
           --- name: "markerOne"
           |
           --- dateCreated: 1674178220
           |
           --- lat: 31.12345
           |
           --- lng: -31.12345

And to query such a structure, the following query is required:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference markersRef = db.collection("markers");
Query queryByCountryAndStateAndCity = markersRef
                   .whereEqualTo("countryName", "USA")
                   .whereEqualTo("stateName", "Illinois")
                   .whereEqualTo("cityName", "Chicago");
queryByCountryAndStateAndCity.get().addOnCompleteListener(/* ... /*);
  • Related