Filtering By Distance
Great! You now have a procedure that can work out the distance between two addresses. Next you’ll add this to your when FireBase.GotValue function.
For this you of course need two addresses: the address of the accessible place and your address (the address of the app user).
- First you should get your current location. As you will be using this value multiple times, it’s a good idea to store it as a variable. Add an
initialize global name toblock, change its name tocurrentLocation, and set it to an empty text block.

- In the
when ListOfPlaces.Initializeblock, add aset global currentLocation toblock and connect it with aget LocationSensor.CurrentAddressblock.
But what if the user’s location is unavailable? To cover this possibility, you need to do a check before setting the currentLocation variable.
-
Put an
if thenblock intowhen ListOfPlaces.Initializeand move theset global currentLocationcode into thethen. -
Find the
LocationSensor.HasLongitudeLatitudeblock and attach it to theif:

Now you are ready to use the procedure you made to get the distance.
-
Insert an
initialize local name toblock (the one with a top attachment) into thethenin yourwhen FireBase.GotValue, and changenametodistance. -
You only want to use the distance formula if you know the user’s location, so connect an
if then elseblock (the one that has a side attachment). To theif, attach three blocks:not(Logic),is empty(Text), andget global currentLocation. -
Connect a
call distanceBetweenblock to thethen. If thecurrentLocationis blank, you will just show all the places, so place a0Math block in theelseto return zero as the distance.

-
For one of the parameters (values that you pass to the function) of the
call distanceBetween, attach aget global currentLocationblock. For the other parameter, attach aget valueblock (remember this contains the address of the place you got from Firebase). -
Inside the
initialize local distance toblock, add anif thenblock.
You are now going to check whether the distance is less than 5km.
-
Get a
<block and a a0block from the Math section. -
Put a
get distanceinto the first input in the<block, and the0block into the second input. Set the0block to5. -
Plug the
<block into theif thenblock. -
Move the
add items to listblock so it is inside thethenstatement of theif thenblock. -
If everything has gone correctly, it should look like this:
