Location based searching?

Hey Team,

I’m trying to build a webapp that shows items based on location data. I’d be storing the latitude and longitude for an item, then using the users own latitude and longitude to query for items close to them, sorted by proximity. Fine for it to use a square/rectangle for proximity because the results will be shown on a map anyway.

Has anyone done this? I figured it would need to be a Custom Function resolver, but I’m not sure how to go about it. I also knwo that the stored format is important for efficient querying of lat/long data.

I’d be using Google Places API to get the items lat/long. Probably the HTML5 Geolocation API for the users location.

Thanks!

I don’t think you need a custom function for finding data, it’s all doable with GraphQL I think. As for searching by location, I assume you’ll need some kind of hierarchy/buckets to narrow down the data, then just do a distance test. I’m just thinking out loud, but maybe something like this:

A square range of latitude and longitude, maybe simply divided by 10 and save that as the “sector”. Then load all the other sectors surrounding it. That will give you narrowed down data instead of querying the entire database. Then you can do a distance test comparing each item. This will need to be done on the client, since it requires some logic.

|-------|-------|-------|
| 09.01 | 09.02 | 09.03 |
|-------|-------|-------|
| 10.01 | 10.02 | 10.03 |
|-------|-------|-------|
| 11.01 | 11.02 | 11.03 |
|-------|-------|-------|

So say person is at long 101, lat 21. That would be sector 10.02. Load all the other data in those 9 surrounding sectors and do the radius test for each one. If there is lots of data, you can do multiple hierarchies of broader ranges. Is it just the US? How many items per square mile/km?

query {
  placesList(filter: {
    sector: {
      OR: {
        sector: {
          equals: "09.01"
        },
        sector: {
          equals: "09.02"
        },
        ...
      }
    }
  }) {
    items {
      lat
      long
    }
  }
}

This is just straight out of my head. You can probably find code to do this, but you’ll need to adapt it to work with your data. I realize this might not be that helpful being kind of vague and missing details, but a start maybe?

Similarly to @mike_ekim1024 suggestion you could filter between a square/rectangle.

{ filter: {
    lat: {
        lte: NORTH_BOUNDARY_LATITUDE,
        gte: SOUTH_BOUNDARY_LATITUDE
    },
    lng: {
        lte: WEST_BOUNDARY_LONGITUDE,
        gte: EAST_BOUNDARY_LONGITUDE
    }
}}

Yeah, maybe I was over-optimizing. I really don’t know how efficient a DB is at comparing lots of number ranges. I do know you can use simple collision detection to filter by radius, but then again, your view is a rect.

Hi @drewbaker!
We’re going to implement geospatial types, but it isn’t hight priority task right now.

Still you can try two options to implement this by yourself. As you mentioned, it can be nailed with custom function. Take resolver (https://docs.8base.com/8base-console/custom-functions/resolvers), and implement in it formula like that: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator#finding-locations-with-mysql. Pass own latitude and longitude as input arguments and query for items inside the function.

Thanks everyone for the ideas! I’ll post back with where I end up.

1 Like