HOW TO: use LocationService extension Version 1 and Version 3 with a GoogleSpreadsheet to record real time locations while the app is in the background

Trak_It Trak_It HOW TO: use LocationService extension with a GoogleSpreadsheet to record real time locations while the Trak_It app is in the background (also works in the foreground).

Edit Sept. 2022. Be aware the example Blocks does not work with Version 2 of the Location Service extension. The example code only works if you use Version 1 and nb189 and Companion 2.64. :cry: @Taifun now provides Blocks showing how the tutorial can be used with Version 3 of the LocationService extension (see the example at App Inventor Extensions: Location Service | Pura Vida Apps )** and a Google Sheet. :slight_smile:

Trak progress over your route using your Android cell in real time using the location sensor built-in to the extension, even when the app goes to sleep using Taifun's LocationService extension . Use Tim's advice showing how to use PUT with Google Sheets step by step to post the coordinates to the Google Sheet .

Here are all the Blocks. Really!

The example uses a paid extension; sorry no aia. Create an appropriate spreadsheet following Tim's instructions. The example app is an adaptation of the template aia Tim provides modified to use the Blocks from the extension. A similar app to post coordinates can be built without an extension using native controls. See the comments at the end of this tutorial; it will work only in the foreground.

Build the Beacon app. How to do much of this is described in detail in Tim's link.

You need to

  • create a Google Spreadsheet on your Google Drive
  • name the spreadsheet and add a script using the script editor.
  • save the spreadsheet with Sharing.
  • obtain and load the Location Service extension. This is a paid extension.
  • add the Blocks and note the example is similar to Tim's app template aia but is very different too. Follow the Blocks to build the app.
  • a WIFI or network connection (to access the spreadsheet).

When you trak and the service is engaged, the location sensor in the extension determines your location. It posts (Broadcasts using a PUT) the location coordinates and a timestamp continuously to a spreadsheet. See Taifun's documentation showing how to do something similar using MySQL. The app acts as a location Beacon; posting data to a Google spreadsheet hosted on your Google Drive.

Monitor the movement (location coordinates) of the Android that is running the example code in real time by observing values posted into the spreadsheet.

Easily use your PC to view the spreadsheet as it updates or use a separate Android app that monitors the spreadsheet and posts the data to a map. Yes, you need to build the monitoring app using the advice in Tim's PUT document (see link above).

The app posts location changes every 30000 ms (1/2 minute). I used this interval to keep the testing times to a minimum and demonstrate the extension really works with the spreadsheet. Latitude, Longitude and a timestamp is provided. In the real world, there is usually no reason to post this frequently, the app will use up a lot of bandwidth if you are using network to access the Web and post too frequently.

Remember to copy your Spreadsheet script link to the scriptURL block when you code the Blocks.
Label1 and Label2 allow you to monitor the present location coordinates while the app is awake.
The Button1 event handler configures the location service and initiates the tracking.
The Button2 event handler stops the app from tracking. Use Button2 to shut the app down; without it, the app will continue to trak until the Android operating system shuts it down.

Does the app sound an alarm when it reaches a destination. No.
Does the app make a map. No, it only beacons. You can add a map to display while the app is awake.
Does it track and post coordinates with the app running in the foreground or background? Yes but the service must be turned on (Button1).

The example provides the minimum code it takes to run and post coordinates to the spreadsheet while the app is in the background and running the service. Add you own enhancements.

Enhancements:

  • add a Map component to show the current location or a LineString trak of locations on the map. Use a Marker or a LineString of the example Trak_It Beacon app when the app is in the foreground.
  • create a separate Monitoring app to poll the spreadsheet. Use the data to post current location on a map. You can't use the service Broadcast app (how can you view it when it is hibernating?) so you might need a separate monitoring app on a separate device.
  • Determine when you are close to or reached a destination. Code a ring fence or calculate a distance remaining to a specific location on the Monitoring app.
  • add more data fields to your spreadsheet. Currently LATITUDE, LONGITUDE and DATETIME are posted. You can also add CURRENTADDRESS. (See Taifun's example using a MySQL spreadsheet and a Google Spreadsheet)

Thanks to @Taifun and @TIMAI2 for the tools to make this app example possible.

Tested Version 1 of the extension on an Android 8.1, 11 and 12. I let the app determine location while the app was in the background for over an hour in my tests. It did indeed work. I only tested with WIFI. Will the app work with network? Probably. I get a similar app working using Version 3 of the extension. :slight_smile:

Consider using the example app along with another user who has a Monitoring app capturing the spreadsheet data ( in a CSV file and posting to a map perhaps.

Other resources:
Using extensions App Inventor Extensions

Other options:
Use a regular LocationSensor and a StayAwake routine and the Web component to make your app. An app made that way can share the 'LocationService' spreadsheet you use with Trak_It.

Here is how you might code a Stay Awake block routine using a Notifier

or you could use Taifun’s Tools Extension: https://puravidaapps.com/tools.php

Using either an extension or a block routine drains your device battery rapidly . These work around methods are not running the app in the Background, they force the app screen to stay open .

===========================================================================

WARNING Google considers apps that developers use with Sharable Google Sheets on Google Drive a security issue. Is it? You have to decide. Use at your own risk. I am comfortable using this for my own use.

===========================================================================

Regards,
Steve

4 Likes

Hi I cannot see the .Post function when following your example. Am I doing something wrong?

webPT

I don't know, you need the PostText Block if you do not use Taifun's extension but elect to use the MIT LocationSensor in it's place. See

Other options:
Use a regular LocationSensor and a StayAwake routine and the Web component to make your app. An app made that way can share the 'LocationService' spreadsheet you use with Trak_It.

Hi i followed the example blocks that you posted for Trak_it and I don't have a post function from the paid locationservice extension thats why i originally asked. Since then, I've played around numerous times to get something to post to my sheet with 0 result. Please see latest attempt below, if you could help i would greatly appreciate it.

Check the Syntax of your query... there seems to be a double quote too much...

From the documentation

The following keywords are available: DATETIME, URIENCODEDATETIME, LATITUDE, LONGITUDE, ALTITUDE, ACCURACY, SPEED, CURRENTADDRESS, URIENCODECURRENTADDRESS and PROVIDER

.

Neither the keyword URIENCODEDATE nor URiIENCODETIME exists..

Also requesting permissions is an asynchronous process... I already mentioned in the other thread, that requesting permissions and at the same time starting the service will not work ...

First request the permissions and make sure the permissions have been granted before starting the service... see again the example project...

Taifun

1 Like

The PostText Block is not used Samuel. All the work is done in the apps script.

The app works in Android 8.1 as described; however I **

Did you try the example Blocks? Do they run and your Blocks do not?

**

Hi Steve I tried your example blocks and already have a script that works and is sharing ect. it didn't work thats why i've been playing around to try get it to work. i only want to send date, user,time and locationto my sheet but for the love of money it wont work. what you see above is my 20th attempt at trying to get some kind of post to work using other ways. my extension from Taifun doesn't have the POST function block and as i'm a complete novice telling me what not to do isn't helping. Please help. i have asked numerous times but keep getting told to look at the example aia file that sends to email. not what i need so it makes it worse for me as i don't understand it..

I can't respond right now. Let me think of something.

no worries thank you

Yes, he does.

This Block will allow you to use a user name by adding a new item to the list. The Dummy Name is UserName. You can change the name for each user of your app using Blocks to Samuel, Steve etc. if you set a variable in a modified version of this code to allow the user to use a TextBlock to set the appropriate UserName.

When I use this script in your Spreadsheet,

//This script is bound to the spreadsheet. It therefore uses getActive() to get the

//spreadsheet it is bound to, and getSheets()[0] to get the first sheet in the spreadsheet

var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];

//POST function appends all data sent as parameters to the next row on the sheet, in the order sent

function doPost(e) {

var data = JSON.parse(e.postData.contents);

sh.appendRow(data);

return ContentService.createTextOutput(data);

}

//GET function returns sheet values as a stringified JSON

//Using getDisplayValues() returns all values as strings

//This is not an issue in AI2 as number=string=number

//Use getValues() if you want original value formats

function doGet(e) {

var data = JSON.stringify(sh.getDataRange().getDisplayValues());

return ContentService.createTextOutput(data);

}

you should see something like:

username2

in your spreadsheet.

Hope this helps. Sorry, I cannot debug YOUR code; Taifun might help. You are doing a lot of things that are not possible with what code is used in the tutorial.

Also note, the code will not work on your device if it is Android 10+unless you set the Location permission manually using the system Settings for your device probably for Android 10+ . The tutorial mentioned it works on Android 8.1 and it does without asking for permissions.

@SteveJG is still using version 1 of the extension...
Of course there still is a POST method available also in version 2, it only looks a bit different...and you are using it correctly...

You are on a good way already with your blocks.. why not adjusting your blocks as I already suggested earlier?

Taifun

ok so I'm officially at my wits end, I've tried looking at the example aia file which isn't helpful, I've played around with the block that many times I've lost count. Could some look at my blocks and tell me how to enable it to post to a google sheet in the background. Nothing i do seems to work in the slightest.

Hi Samuel.

  • @Samuel_Dutton did you test the Post as suggested by Taifun? I Loaded extension version 2 and my spreadsheet test works. Does your testing of Post work? This code posts the words LATITUDE and LONGITUDE to columns 1 and 2 or the spreadsheet; however it will not post the actual latitude and longitude.
    postToSpreadsheet . This confirms that the POST works when used outside of the Service code.

  • The code example I show in the tutorial works with extension version 1. I originally tested it with Android 8.1; later I tested it with Android 11 too . Version 1 worked fine and it works with Android 12 also.

  • I cannot get extension version 2 to work with my Android 12 cell. What is frustrating is that I can get the app working using version 1. A suggestion is for you to ask @Taifun to kindly provide you with a copy of Version 1. Use my example code with version 1. After you get the simple app working, save a copy of the Project then add your ring fence code.

  • at the present time, I too can not figure out how to use Version 2 of the extension (what you have) with a Google Sheets script. When I compile, I get errors from Google's Play Protect saying there is a bug in the app. The problem seems related to his substitute Location Sensor or something else or issues in my coding.

  • Perhaps Taifun could show both of us some simple Version code to post coordinates to a google sheet. I have given up using Version 2 since Version 1 works for my purposes. :cry:

That's the same problem I'm getting. I cannot get it to post unfortunately. I'll ask for version 1 from Taifun if he would kindly do so.

Taifun as kindly given me version1, if its not to much to ask Steve could you send me a copy of your aia file and script as mine won't post? I've double checked the script, copied yours ect I know it's frustrating but would be a big help. I'm starting to think it's android version 12 that's the issue.

Great :slight_smile:

I don't think so. My app compiled with nb189 and Companion 2.64 works fine. My Samsung A13 updated to Android 12 on August 24. The app I compiled prior to MIT's nb190 and Companion 2.64 works FINE on my A13; using the identical code after the 24th no longer works properly. It compiles but it does not run in the background.

I suspect the issue is it does not have a Background permission.

@taifun, are users of Version 2 reporting any issues running on apps compiled using nb190?

A guess is nb190 requires either a Background permission or a simultaneous Coarse and Fine Location declaration (see GPS with Android 12 - #42 by ewpatton ) . I really don't know.

@Samuel_Dutton If you add a Button and the following to the app, does it post on your spreadsheet?
postLocation
When I add the code it posts with either LSVersion1 or 2 and provides a test checking your script and spreadsheet. However the service never starts in either my Version 1 or 2 app using nb190.

Sorry I cannot provide any additional help. I am going to see if I can find a copy of Companion 2.64 and see if rolling back to it will allow the Version 1 extension to work properly. At the moment, recompiling my Version 1 app does not work but prior apk's work fine.

I will investigate what is going on and let you know soon... most probably something has to be upgraded after we now are using SDK31...
Taifun

this is the error after the SDK31 release
Unbenannt
I'm working on a fix...
EDIT: this is the same issue which has been reported here Runtime Error: Targeting S+ (version 31 and above) requires that one flag of FLAG_IMMUTABLE be specified[...] - #8 by ewpatton

Taifun

2 Likes

how to get version 3?

This is a paid extension. You will have to buy it from the extension author.