Search code examples
firebasegoogle-mapsgoogle-cloud-platformgoogle-cloud-firestore

Database structure when creating a map app


I want to display nearby points stored in a database on a map. However, there are over 1000 points, and I am having trouble figuring out how to construct that data.

For example, if you search for "McDonald's" using the Google Maps app, it will show nearby McDonald's instead of showing you a McDonald's 1000 km away.

I'm trying to use Firebase's Cloud Firestore for my database, is this appropriate? Do you think Cloud Firestore is not suitable for this because of the payment per document?

One of the data is like:

{
  "name": "1",
  "cord": {
    "lat": ....,
    "lon": ....,
  },
  "type": "pin",
  ....
}

Solution

  • However, there are over 1000 points, and I am having trouble figuring out how to construct that data.

    It's not about how to structure the data, as it's about limiting the number of documents a query returns, by calling limit(x).

    For example, if you search for "McDonald's" using the Google Maps app, it will show nearby McDonald's instead of showing you a McDonald's 1000 km away.

    That's correct. You can achieve the same thing also with Cloud Firestore. While @Julian answer might work, please note that his solution is no longer relevant because Firebase recently (4 months ago) added the ability to have range conditions on multiple fields in the same Firestore query.

    So to achieve this, I recommend you see:

    Besides that, I also recommend you read an excellent article written by @FrankvanPuffelen which compares the cost of geohashes vs. the newer approach with range conditions on multiple fields in Firestore: