Go back to 8base Academy
August 11, 2022

Filtering Records based on Distance from a User's Current Location

Sebastian Scholl

Sebastian Scholl (00:01)
Hey everyone, Sebastian here from 8base. In this video, we're going to look at how you could do a distance-based query for filtering records based on a user's current location. Let's jump into it.

Sebastian Scholl (00:12)
The first thing just worth talking about really quickly, is how you might actually procure the user's current location. The answer to that question is it really depends on the type of application that you're building. There's one way of doing it if you're working with a browser. There's another way of doing it if you're building a native mobile app, a smart watch application, all of the above.

Sebastian Scholl (00:32)
However, most devices that you are deploying or developing applications for provide some type of API that you can easily procure the user's current location or any other type of geo-coordinate data in a permissioned way.

Sebastian Scholl (00:47)
For example, here, if I open up my Inspector tools in a Chrome browser, I can drop in a small function using the navigator API. So call them navigator.geolocation.getCurrentPosition and then give it a callback function that will handle the response as this is an asynchronous call. I'm going to run that. And it usually just takes a few seconds to get the geodata, or the Lat long coordinates back of the geo-coordinate data.

Sebastian Scholl (01:15)
There we go. It's giving me my current Lat longitude and giving me an accuracy metric. Knowing that we can procure that information, now we know that we can use it as the input to a query. And the query that we are going to write here is one that says, "Okay, well, here is my current location. Show me all the records that are within a certain distance of me." And that is because on those records, we've created a geo field where we are also storing Lat long coordinates.

Sebastian Scholl (01:46)
Let's just go through the steps of writing out one of those queries. And in this workspace, we have a simple data model, which is restaurants and reviews. A restaurant has a location, which is the Lat long coordinates of its location on the map.

Sebastian Scholl (02:01)
Here we go. First off, we're going to write a query. The input to this query is going to be the userLocation or the variable that we pass it. And that is a GeoJSONCoordinates type. Pretty sure it's Coordinates; if we get an error, we will fix it. So that's our input.

Sebastian Scholl (02:23)
Now, we are going to look a list of restaurants. And here, we want to filter those. Now, when we go into the filter—which filter we're filtering by—we want to filter by the location and that is our geo field. Now inside of that, we have a number of different options that we can filter geo fields by. We're just going to work with points here. No bounds, nothing like that. We're just going to go to distance. So we're going to say, distance.

Sebastian Scholl (02:54)
Now, that distance has to be from a certain point that we supply it. We're going to open up from and say, "What type of geo-coordinate data is this?" And it's going to be a point. So here, we're going to say point. And then we are going to say, "Okay, well, here are the coordinates." And that's where we're going to bring in that userLocation. So drop in there. And just know that the variable we're passing is just right here. So we're passing it those coordinates.

Sebastian Scholl (03:29)
Now, inside of distance, we now specify the from parameters. Now, we would need to specify value parameters. The value is essentially the predicate we're going to apply. So there's a distance, and we could want it greater than, less than equal to this distance. So we are going to say that we are looking for all the restaurants that are less than a certain number away from the user's current location; less than 5. So we're going to calculate a distance of 5. And that we want all the records that are less than that.

Sebastian Scholl (04:06)
Now, the last thing that you might guess is 5 what? This is where now we need to supply a unit of measurement. And we support meters, kilometers, and miles. In this case, we can say, "Hey, we want all of the restaurants that are within 5 miles of the user current location."

Sebastian Scholl (04:25)
And then in the response, we're going to say, "Hey, we want to know how many restaurants there are. As well as for each of those restaurants, we want the ID of it, we want the name of it, we want the location data that passes back. And then we just want to know how many reviews they each one of those have."

Sebastian Scholl (04:46)
And on location, that is also we need to specify the type. We just want the coordinates back. So there's our query. Let's run it. There we go. Cool. It's returning 11 records that are within the proximity of user that we specified. And if we brought that down to less, let's see. We just filtered it down farther by lowering that number so we know it's behaving as expected.

Sebastian Scholl (05:16)
I hope that this gave you some fun ideas on ways that you can leverage the graph or API if you're building applications that do deal with geolocation data. That said, there's a lot more depth to the geolocation feature or geo field feature on the API to where you can do calculations based on bounds. We can do multipoint calculations, lot of different stuff. We'll explore those in future episodes. Thanks for watching and see you in those future episodes.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.