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 theseaddress1
andaddress2
.
- Next, from the Variables section, drag out a
initialize local name to
block (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 local
block. -
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.LatitudeFromAddress
blocks. Attach aget address1
block to one, and aget address2
block to the other. Put these into thelat1
andlat2
attachments. -
Repeat the same thing with for longitude.
-
Drag in two
0
blocks and attach these toinitialize local x to
andinitialize local y to
blocks.
- Get a
do result
block from Control, and put it into thein
attachment of the (now very big!)initialize local
block.
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 lat2
andget lat1
block into the-
block, and place the-
block into thex
block.
Now you’ve got the difference in latitude!
- Multiply this by
111
to get the distances in kilometres between the two latitudes. Then just plug that into theset x to
block, and put theset x to
block into thedo
section of thedo result
block.
- Do the same thing with the
set y to
block, changing111
to89
andlat
tolong
.
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 root
and+
blocks along with two^
(power) blocks and two0
blocks. -
Put a
get x
into the left input of one of the^
blocks, and put aget y
into the left input of another. Put the0
blocks into the^
block also, with0
changed 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 root
block. Finally, plug this into the result attachment.
Here is what your finished distanceBetween
procedure should look like: