Implementing a Proceedure to Work Out Distances
Now that you know how to work out distance, you can make your app only show places that are within 5km of a user.
- Drag out a procedure block (the result one) and call it
distanceBetween. Click on the wrench icon in the top left-hand corner and drag two inputs into it. Name theseaddress1andaddress2.

- Next, from the Variables section, drag out a
initialize local name toblock (the one that has a side attachment instead of a top attachment).

-
Add six variables to this:
lat1,long1,lat2,long2,x, andy. Use the same method you used for the procedure, by clicking on the wrench icon in the corner of theinitialise localblock. -
Now you need a way of converting text addresses into latitude and longitude coordinates. Thankfully, the LocationSensor does this, so go to the Designer view and add one.
-
Get two
call LocationSensor.LatitudeFromAddressblocks. Attach aget address1block to one, and aget address2block to the other. Put these into thelat1andlat2attachments. -
Repeat the same thing with for longitude.
-
Drag in two
0blocks and attach these toinitialize local x toandinitialize local y toblocks.

- Get a
do resultblock from Control, and put it into theinattachment of the (now very big!)initialize localblock.
Great! Now you need to work out the distance.
+Get out the blocks set x to, get lat1, get lat2, x, -, and 0.

- Place the
get lat2andget lat1block into the-block, and place the-block into thexblock.

Now you’ve got the difference in latitude!
- Multiply this by
111to get the distances in kilometres between the two latitudes. Then just plug that into theset x toblock, and put theset x toblock into thedosection of thedo resultblock.

- Do the same thing with the
set y toblock, changing111to89andlattolong.
Perfect! With that you have the lengths of two of your triangle’s sides to use in the distance formula!
-
From the Math section, get the
square rootand+blocks along with two^(power) blocks and two0blocks. -
Put a
get xinto the left input of one of the^blocks, and put aget yinto the left input of another. Put the0blocks into the^block also, with0changed to2.
This will square both x and y (x squared is x times x, meaning x^2 = x * x).
- Place both
^blocks into the+block and attach this to thesquare rootblock. Finally, plug this into the result attachment.

Here is what your finished distanceBetween procedure should look like:
