Send current location to database every 10seconds

Hello good day can someone help me with my activity? I cant seem to send my current location to my database. I am using firebase as my database. Thank you

Dear @enzo1, welcome to the Community.
Honestly it's not so clear to me the aim of your algorithm.
When you hit button 1 you enable the clock, which fires every "nnn" milliseconds (how many ms have you set ?), when it reaches the 10th tick, the first element of the list is stored into the firebase, then for the remaining 50 ticks nothing happens. When the clock reaches 60, it stops itself.
And that's all.
The 2nd, 3rd, 4th elements of the list are never used, nor stored into the database.
Is this what you really want ?
Another hint is to avoid comparing the content of a label (which is a text) with a number. It is better to compare "things" of the same type. Moreover you have available the tick counter, the variable Timer, which can be used instead of Label3, in other word: you can leave Label3 to show the tick counter increments, but using the numeric variable Timer to perform the comparison.
Cheers.

Welcome Enzo.

  • most people use a LocationSensor to determine their current location
  • every 10 seconds? Why so often?
  • why are you using a List (called 'List') to store location coordinates. to almost 14 decimal place precision?
  • your Button1 always sends the position of the first coordinates in the list. Is that what you really want to do.

Here is a way to send a location to a CloudDB (similar to a FirebaseDB) How to broadcast a GPS location and share the information on a map with others in real time..CloudDB . The example might help you do what you hope to do.

I want to send my current location to the firebase db every 10 secs. Can you help me with that? Thank you

As @SteveJG has said, typically to get the location information it is used the LocationSensor, then you can set a clock to store the Lat & Lon values on firebase.
The LocationSenor is an "invisible" component that you have to drag-and-drop at design level, then in the Blocks you shall enable it as follows:
image

In these blocks I've set the interval between two consecutive samplings at 1 second (1000 ms) which is the fastest repetition rate at which the GPS can be sampled (please be aware that the faster is the sampling rate, the higher is the battery consuming).
I've also set the delta-distance to 0: this means that the GPS does not wait a certain amount of delta-meters to issue a new fixed value. With these settings I can obtain updated values quick enough to follow a vehicle whose speed is > 100 km/h. Probably it is too fast for your use, but you can trim the timeinterval at your effective needs.
The clock is also set to fire every 10 seconds, and enabled but it is not allowed to work in background to avoid unexpected ticks.
Please also remember that the GPS shall be enabled at Android level (enable the fine-location in Android settings). The LocationSensor works better in open-air: if you test it in a closed environment, probably it will take few minutes before fixing a position (or even it can't).
To get the Location values you have to code the LocationSensorChanged event, like the following:


Before validating a new position, I typically check the Accuracy: the lowest, the best (but not = 0 !!!).
In my code, when the LocationSensor Accuracy is < 20 I consider it good enough. Optimal values are around 4 or 5, but these low values can be reached only after the Sensor has fixed at least 8-10 satellites.
The clock firing event could simply be something like:
image
This means that if a new couple of values havs been fixed, it is transferred to the firebase, else the previous one is left (and not overwritten).

I haven't coded the procedure FirebaseStore, you can do it by your own, following the instructions that Steve gave you in his post.
Best wishes !!!

You could try this (uses HTML5 GeoLocation):

GeoLocation10Secs.aia (4.7 KB)

1 Like

What you do depend

  • on your reasons for storing your location coordinates to a FirebaseDB -- as rapidly as every ten seconds. That stresses your gps hardware and Android cpu and battery.
  • on how you store to the database. It is important whether you want to merely post your present coordinates or store all your previous coordinates as a List under a single Tag or store as individual Firebase Tags. Storing every 10 seconds creates a heavy burden on your device cpu and will eventually and very quickly create a huge List if you store previous locations.
  • pm whether each set of coordinates should have a timestamp (provide information about the time/date the location was posted) or not.
  • on whether you intend to share the data with others.
  • on whether you get the coordinates from a gps or other source
  • on whether your app is allowed to go to sleep or whether you force it to stay
    awake using code or an extension to run continuously for extended periods of time. App Inventor apps do not run in the background (unless you use an extension) and will not record your coordinates when your Android goes to sleep.
  • on whether you also display coordinates on a Map or not.
  • on whether you are using Android 11+ or older Android (an issue of permissions)
  • and lots of other issues, depending on what you will do with the information after you send it to the database.

Only if you explain in detail what you hope to do. :slight_smile:
What you coded so far is not the way to code your app.

Sending the actual coordinates to the FirebaseDB is relatively simple. The other issues are in the details and make providing you advice very difficult. Your request is too vague. :cry:

1 Like