Android Question waiting until multiple events occured

zohar

Member
Licensed User
Longtime User
Hi Guys

i need an idea how to do the following and i dont know where to start .
ive build a simple gps application which recognize specific place , but i can't go any further .
i would like to perform the following :
check if im in the "Right" location (based on location in array)
if so , start check events one after another for this location .
for example check if the time waited from arrival to this coordinates is more the 20 sec then print something .
then check if orientation change to north and print something .
do it for all the events for this location .
then wait untill user come to location 2 and do the same .

the following is pseudo xml/json file (can be db also)

location 1
coordinate 33.33
event 1 , wait 20 sec , print you wait 20 sec
event2 , change orientation north , print you looking north now
event3 ..
...


location 2
coordinate 45.13
event1 wait 10 sec , print you wait 10 sec
event2 wallk ten steps , print you walk 10 streps

location 3 ...
.....

** if its preferred , i can also pay for someone who will create the skeleton for it and i will continue from there .

Thanks ,
Zohar
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You need to maintain the current state. I would have created a custom Location Type that holds the location information and a list of events. Event should be a custom type as well (or a class).

One of the location fields should be the current event index. This way you can know at any point what is the next event that you are waiting for.
 
Upvote 0

zohar

Member
Licensed User
Longtime User
You need to maintain the current state. I would have created a custom Location Type that holds the location information and a list of events. Event should be a custom type as well (or a class).

One of the location fields should be the current event index. This way you can know at any point what is the next event that you are waiting for.


Thanks for the quick reply .
just to see if i understand it correctly .
"create a custom Location Type that holds the location information and a list of events"
* this mean that it holds a single record from array ?
* on each location there could be multiple event type (orientation ,distance,sound etc ..) should i "listen" to all the sensors all the time ? or only start "listen" to the next event type .

what i thought i should do , but it become complicated i couldnt handle it is to create large complicated array with nested arrays from the xml ,
and using loop advance and check each event , but its so complicated and i can't make this code scalable.
*** if you can give me with a short example even with pseudo code what you mean it would be awesome .

Thanks again ,
Zohar
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Sounds like a game...

I would create a simple list with the "game tasks" and a script-engine (sounds worse than it is).

Every task is printed (like "Now go to xxx.yyy.zzz")

F.e.:

The list:

"go to", "xxx.yyy.zzz (=coordinate/location)", 20, check_coordinates
"turn north", 10, check_direction

The 20 or 10 is the time to wait for a check if the task is done, the "check_coordinates" or "check_direction" are subs with the functions called afte x secs.

In ONE timer you can check after x secs which sub has to be called.

In the sub you can set true/false if done.

If true then next_task
If false then repeat_task

In the "engine" you just handle the tasks and set it to finished, next, repeat.
 
Upvote 0

zohar

Member
Licensed User
Longtime User
Sounds like a game...

I would create a simple list with the "game tasks" and a script-engine (sounds worse than it is).

Every task is printed (like "Now go to xxx.yyy.zzz")

F.e.:

The list:

"go to", "xxx.yyy.zzz (=coordinate/location)", 20, check_coordinates
"turn north", 10, check_direction

The 20 or 10 is the time to wait for a check if the task is done, the "check_coordinates" or "check_direction" are subs with the functions called afte x secs.

In ONE timer you can check after x secs which sub has to be called.

In the sub you can set true/false if done.

If true then next_task
If false then repeat_task

In the "engine" you just handle the tasks and set it to finished, next, repeat.


Thanks for the explanation

i started to understand how to check the events inside coordinate , however how do i check that im still in the coordinate ?
if im not in the coordinate and there are still events then it should show me that i need to return to it .
is there a way to check the specific coordinate even if i inside sub which check other events ?

Thanks again
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
For GPS and how to use it take a look here: https://www.b4x.com/android/forum/threads/gps-tutorial.6592/ and check other threads.

In GPS there is a sub called for "LocationChanged"

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    'Example    
    If Location1.ConvertToMinutes(Location1.Longitude) > WantedLongitude then
       IamOutOfLocation=true
    END IF
End Sub

It is called when your location/position has changed. It's up to you to check here if it's relevant (as you say "still inside" a corridor). If inside then set a flag to true or false if not (or use other variables). To check if you are "inside" compare the coordinates with the coordinates of your corridor). Play arround with Lat ><= and Lon ><=. Take a walk outside and see how it changes when you walk.

It's like standing on a cross you've painted on the ground at a position Lon(gitude) x and Lat(itude) y. If you walk, the values change. I don't know how much it changes f.e. 10m. Try out. So you can see what to use. Google Maps (Browser) can handle coordinates, too. Zoom in your street and get the values for a square (20x20m). If the Lon/Lat is outside of this square, you are out of it.

The rest is up to you (check the position and set variables and/or show a message like "go back to x"). For sure there are examples in the forum calculating the distance from a to b (like "walk 10m north").

Define a Timer and in the sub called by it, check the variables you have set in Sub GPS_LocationChanged
 
Upvote 0
Top