Use the Android GPS receiver using a Foreground service, capture locations and a make a Track. Three examples use the CloudDB, a fourth example uses a Google Spreadsheet to store the data.Create a List of location coordinates and display a Track on a Map.
When the Foreground (KAS) service is activated:
- the app will continue to post to the database when and after the device screen closes. The data collected becomes visible when requested by the user.
- **users are able to work with other apps while the Service app is active. Even though another app may be active, the Foreground will continue to post to the appropriate database.
Experimental Foreground Version' aia's (aia's are at the end of the tutorial):
- FG_GPS_Sheet Use with a Google spreadsheet. Uses KIO4_LocationSensor extension
- FG_GPS_Cloud_LS Use with a CloudDB and the native LocationSensor
- FG_GPS_Cloud_KIO4 Use with a CloudDB and the KIO4_LocationSensor extension
FG_Location_HTML Use with a CloudDB and a location html and WebViewer (no sensor component). HTML5 geolocation detects latitude and longitude coordinates by using the device's GPS (if available on the device) or the device's mobile/WIFI signal (if GPS is not available . The mobile/WIFI signals are triangulated to work out the latitude and longitude.
Locations may not be as precise as using the gps hardware alone.
- All versions: the aia MUST be compiled and then loaded to your Android in order to work as a service. You MUST have a WIFI or DATA connection to access the databases.
Cloud versions: none; the aia contain the required extensions.
HTML version: none other than a location html file in Media.
Spreadsheet version: a configured Spreadsheet on your Google Drive. Sorry I do not share my Google Drive with others as a security issue. A link is provided to Tim's tutorial showing how you can set up a spreadsheet in your Google Drive and provide the required spreadsheet and Share it as described in Tim's tutorial.
How to use the compiled apps
The app examples can be used as a conventional App Inventor app or as a Foreground Service. Each app has a slightly different UI as needed for use with the app's components.
To load and use the app for the first time, I suggest you:
load the aia and build the app. The CloudDB examples will work right out of the box; the developer must provide his/her own Spreadsheet stored on GoogleDrive and provide his/her GoogleDrive settings in the appropriate places in the aia. Yes, you must create your spreadsheet and provide the GoogleDrive link in the Spreadsheet aia prior to compiling the aia.
the first time the app is run, Android will ask you to confirm Foreground and Precision Location.
use the apps StayAlive On button to start the Foreground service.
go for a drive or a walk with your app and create a Trak
you have a choice to record a new Track or add to an existing Track. A Button may be provided to Clear Last used Track . It clears the CloudDb and will allow you to start a new track with the LocationSensor based examples.
you don't have an option to record a new Track with the spreadsheet example (unless you code the capability). You can delete rows manually from the spreadsheet stored on GoogleDrive to clear the stored track's coordinates using your PC. Alternatively, you can provide code to use another spreadsheet page (I do not show how).
turn the gps on with GPS Tracking switch or the Start button. You should now see a small location icon on the upper right corner of your device (showing the gps is listening).
give the app sometime to get a satellite fix and allow the app to store at least two location changes. This process may take as long as two to three minutes! Be patient. A location change is stored once a minute so drive or walk with your Android for a while to partially fill the database. Watch the road, not your phone screen!
after about 10 minutes of collecting location data; access the accumulated locations with the get cloud locations Button on the Cloud versions or with the Get Spreadsheet Tracks Button on the Spreadsheet version. A blue track (LineString) displays the route you followed on the map if your app has captured at least two sets of coordinates.
before leaving the app, make sure you turn off the Service. Use the StayAlive Off button; otherwise the app will continue to run. This image shows how to know if Foreground is active; the app image shows a notification and the bar displays a dog tracks icon.
If GPS Tracking is turned on the current location is posted to the MIT default CloudDB server.
If StayAlive On is set to start the service, the posting continues in the Foreground as a service, even when the Android display closes. Use the StayAlive Off Button and the app behaves as a normal App Inventor app that will stop posting to the database when the app goes to sleep.
The app MUST be compiled and the apk loaded to use as a service. You can test
the basic operation of the app in the Companion but cannot run as a service. When testing, it helps to take the Android device somewhere so the LocationSensor can actually change values, rather than test with the device setting on your desk. Go on a short walk or drive.
Two extensions keep the app alive and adding to the location database.
The UrsAI2KeepAlive zip archive Ullis Roboter Seite/AI2 Keep Alive The archive contains the source code, the compiled binary for uploading to the App Inventor and a discussion of how Ullis' Foreground extensions work.
The KIO4_LocationSensor.aix extension of:
Create Build Crear App Inventor Extensiones Extensions. Sensor de localización. Geo localización. Campo magnético. is included in the aia's.
Users can view the location List in real time by setting Label5 to visible with the View stored locationList switch (the label displays when switch is on and the cloud locations Button is used). Developers could provide a virtual screen to do the monitoring.
With slightly different interfaces each CloudDB app uses either the native LocationSensor, a location html file and no LocationSensor at all.
Track (LineString or Runtime Markers or Both)
The app's default to displaying a blue Track on a Map. Code is provided to display tracks, Markers or both on a Map component within the drawMapTrak2 Procedure. Determine what is displayed by restoring greyed out code or deleting LineString code. The default behavior in the apps is to show only the Track. What to display on the Map is not user selectable from the device screen at the moment; developers have to adapt the code.
You have the option to add to an existing track or start an new track (discussed elsewhere in this discussion) by modifying the code.
Location points are scheduled by Clock 2 to be posted to the database every minute in the spreadsheet example; and every 30 seconds in the three CloudDB versions. You may want to schedule the posting to the database once every 30 seconds to get a smoother track display more compliant with roads and streets if a smoother Track is required.
If GPS Tracking is turned on the current location is posted to the spreadsheet.
If StayAlive On is set to start the service, the posting continues in the Foreground as a service, even when the Android display closes. StayAlive Off and the app behaves as a normal App Inventor app that will stop posting when it goes to sleep.
The app MUST be compiled and the apk loaded to use the app as a service. You can test
the basic operation of the app in the Companion but cannot run as a service while using Companion. When testing, it helps to take the Android device somewhere outside so the LocationSensor can actually change values and create a nice Track, rather than the device setting on your desk.
Two extensions keep the app alive and adding locations to the location database spreadsheet while not displayed (when you are using another app or the screen is closed).
The UrsAI2KeepAlive zip archive Ullis Roboter Seite/AI2 Keep Alive The archive contains the source code, the compiled binary for uploading to the App Inventor and an explanation of how Ullis' Foreground service works.
The .KIO4_LocationSensor.aix extension of:
Create Build Crear App Inventor Extensiones Extensions. Sensor de localización. Geo localización. Campo magnético. is included in the aia's.
Users can monitor their spreadsheet locations posting in real time. Use your PC and view locations posted in real time. To view the spreadsheet you need a PC Browser link to your sheet
Discussions about how to display the spreadsheet data in your app using a Table display are in the community. You cannot monitor the spreadsheet in real time from the Foreground app unless you create a separate app just to display the spreadsheet information.
Tim's How to Create a Google Spreadsheet shows how to design and post the locations to the spreadsheet. The Metric Rat AI2 web site shows how to create and post data to a Google Sheet. The required code that needs to be changed to your sheet specifications (except for the spreadsheet links) are provided in the spreadsheet aia example.
The code that needs to be changed by you:
The dark green arrow shows where you apps script link goes (there are two places in the code that need to be replaced. The light green arrow showa where the File ID for your Shared spreadsheet goes.
See Tim's tutorial please.
The spreadsheet uses a header latitude,longitude,time,id,name ; id and name. The id and name items are for adapting the sheet to your requirements. They are not captured or used in the example app. They are place holders for future use in the app.
Your spreadsheet should look like:
When used in the app, the Web component captures data from all rows except for the header row.
The sheet can look like anything you want but to be compatible with the example, it MUST mimic the Google Sheet shown.
The Foreground Service
All four tutorial examples use the same Block code to provide the Service. The code consists in two extensions by Ullis' UrsAI2KeepAlive and UrsAI2WakeLock, the aia's and the Following Blocks:
Ullis' description of his extension explains what can be done and what cannot be done using the latest Android version.
TFG_GPS_Sheet and FG_GPS_Cloud_KIO4 use the KIO4_LocationSensor to determine present device location; FG_GPS_Cloud_LS uses the native LocationSensor and FG_Location_HTML doesn't use a LocationSensor object at all.
Testing was done using a Samsung A13 running Android 13 software using WIFI and mostly 5G DATA. The testing involved going for a drive outside to test the Service and the track plot for about 10 minutes to ensure enough coordinates are acquired and posted to the database. Be aware, usually it takes up to two minutes to start building a LineString track.
The UI used for the examples
The UI is similar for all the examples. What the UI looks like was initially based on code adapted from an example aia by Juan Antonio refactored for two examples and a different model was adopted for the other two. There are differences in the coding of the location data that is posted to the CloudDB and the Google Sheet. The most significant difference is the apps using the CloudDB store coordinates directly as a List in the CloudDB. The Google Sheet version stores coordinates as a csv file and the app converts the csv file to a coordinate List.
Tools within the App Examples
the CloudDB based apps have a control to erase the existing location Trak. The tool 'erases' the current location data collected; clears the Map of the blue Track and any Runtime map Markers.
the Google Sheet version FG_GPS_Sheet does not have a control to erase the existing map (it needs modification of the app script to clear the database and code to remove Markers).
provision to store the created Track for later use. The examples cannot store multiple Tracks on any example at present. (This could be done with a modification of the app script to add additional spreadsheet pages and switch between the pages to use the appropriate spreadsheet database. Multiple Tracks could be made by saving the locationList to a different CloudDB Tag for the Cloud version.
erasing the last stored Track: Use the 'Clear Last used Track' button before starting Tracking to clear the track; otherwise your 'new' tracks will be added to the last track you made.
FG_GPS_Sheet and FG_GPS_Cloud_KIO4 use the KIO4_LocationSenso include code to calculate straight line distance between a 'goal' and present distance. Code exists on versions using KIO4_LocationSensor but the code is disabled to keep the example simple.
FG_Location_HTML automatically calculates the length of the track you make.
To do list for you
- provide option to save multiple tracks using Tags with CloudDB and different sheets using the Spreadsheet database.
- Save Track coordinates to a File readible on a PC
- create a FirebaseDB version of the app. Basically replace the CloudDB controls with FirebaseDB controls.
- share your modified versions of the Foreground GPS in the community
- make the choice of what map features are selectable from the device screen (Markers, LineString or both). At the moment only a blue track is displayed.
- make a low battery alarm.
- refactor an example's code. Make the app respond as you want it to.
- Create different Foreground apps using the basic framework shown here.
How the app code works
- coordinates are provided either from native LocationSensor or KIO4_LocationSensor or location html
- Foreground Server code (described by Ullis on his web page)
- uses either a CloudDB or Spreadsheet database
- data set for CloudDB locations can be displayed by making Label5.Visible to true.
- data set for Spreadsheet locations is the spreadsheet.
- uses the MIT Map component
- Clock2 provides a posting of latitude,longitude,time,ID,name where the first three values are provided values and ID, are name place holders for developer's expansion if needed. Values are posted to the database once a minute ( 60000 ms ) by a Clock with the Google sheet and 30000 ms for two of the CloudDBs. Developers can post more or less frequently.
- the version using the native LocationSensor has TimeInterval set to 30000 ms; developers can experiment with more frequent attempts to update the location. FG_GPS_Cloud_LS uses the LocationSensor TimeInterval set to 30000 ms. FG_GPS_Cloud_KIO4 and FG_GPS_Sheet update when a location change is sensed and the Clock timer fires as does the FG_Location_HTML example using the html location.
The aia's show the Blocks to code the apps.
Expectations and Limitations
- How many points can be plotted? Unknown. A lot.
- How long can the app run? Unknown. Tested for over an hour. Practically, until the battery runs down. Remember wakelock is suspended.
- The no location sensor object example FG_Location_HTML simplified coding but is slightly buggy.
- None of the examples are 'finished' apps. They all can be improved.
- Acquisition of coordinates is dependent on a WIFI or DATA connection and the ability of the location tool used to capture a satellite fix or cell phone data. Quality of data will depend on whether your device has a 'clear' view of the sky. These app examples won't work if your Android is in your glove box or purse.
- Ullis and the ullisroboterseite for the Foreground aix and source.
- Metric Rat (Tim) for a tutorial to post data to a spreadsheet
- Juan Antonio for his KIO4_LocationSensor alternative to the App Inventor native LocationSensor.
- Tim's location without gps LocationSensor shown at METRIC RAT AI2 - Location Services with HTML5 GeoLocation
Another Project built with Ullis' Foreground extension is 📻 WebView Radio Stream Player
Have fun experimenting. Please report 's
FG_GPS_Cloud_KIO4.aia (84.6 KB)
FG_GPS_Cloud_LS.aia (93.7 KB)
FG_GPS_Sheet .aia (85.5 KB)
FG_Location_HTML.aia (86.8 KB)