Skip to content

Instantly share code, notes, and snippets.

@mbohun
Last active December 5, 2025 10:04
Show Gist options
  • Select an option

  • Save mbohun/b91cc5834e8fdb2137d2427da46af0d2 to your computer and use it in GitHub Desktop.

Select an option

Save mbohun/b91cc5834e8fdb2137d2427da46af0d2 to your computer and use it in GitHub Desktop.
termux; termux-api; termux-location

termux-location

install

  1. install termux app
  2. install termux-api app
  3. open termux terminal and install:
    pkg install termux-api
    

run it

On the first attempt you are going to get a permission error, and that will trigger a GUI dialog where you can allow termux-api to access the GPS subsystem.
Alternativelly you can setup this permission beforehand.

~ $ termux-location
{
  "error": "Please grant the following permission to use this command: android.permission.ACCESS_FINE_LOCATION"
}

use it

~ $ termux-location
{
  "latitude": 48.90542000000001,
  "longitude": 18.187695,
  "altitude": 352.0,
  "accuracy": 5.5,
  "vertical_accuracy": 57.29999923706055,
  "bearing": 289.95001220703125,
  "speed": 0.33714306354522705,
  "elapsedMs": 12884902019,
  "provider": "gps"
}
~ $ echo "Poliklinika TN"
Poliklinika TN
~ $
~ $ termux-location
{
  "latitude": 48.88806833333333,
  "longitude": 18.031871666666667,
  "altitude": 252.20000000000002,
  "accuracy": 6.199999809265137,
  "vertical_accuracy": 21.899999618530273,
  "bearing": 358.1000061035156,
  "speed": 0.6397997140884399,
  "elapsedMs": 12884902040,
  "provider": "gps"
}

test:

REFERENCES

#!/bin/sh
SESSION_DIR=~/record_gps-`date +%s`
mkdir ${SESSION_DIR}
while true; do
loc_json=`termux-location`;
if [ `echo "${loc_json}" | grep -c latitude` -ne 0 ]; then
ts=`date +%s`;
echo "${loc_json}" > ${SESSION_DIR}/${ts}.json;
fi
sleep 3;
done
@mbohun
Copy link
Author

mbohun commented Nov 28, 2025

1000014759

@mbohun
Copy link
Author

mbohun commented Nov 29, 2025

INPUT:

~/record_gps-1764392293 $ ls
1764392303.json  1764392353.json  1764392398.json
1764392316.json  1764392368.json  1764392417.json  1764392328.json  1764392380.json  1764392430.json       1764392340.json  1764392390.json 

Turn the filenames into a JSON array/file:

$ ls *.json \
    | xargs \
    | sed -e 's/\.json//g' \
    | jq -s '[ .[]|{"timestamp":.}]' \
> ts.tmp

Merge the GPS JSON files into one JSON array/file:

$ cat *.json | jq -s > gps.json 
$ mv ts.tmp ts.json

Merge timestamps and gps into one JSON array/file:

$ jq -s '[ .[0][] + .[1][] ]' \
    ts.json \
    gps.json \
> session.json

@mbohun
Copy link
Author

mbohun commented Nov 29, 2025

TODO:

  1. for_each GPS JSON file insert in the timestamp (filename)
  2. Then merge the GPS JSON files into one JSON array/file

@mbohun
Copy link
Author

mbohun commented Dec 2, 2025

Test this:

#!/data/data/com.termux/files/usr/bin/sh

termux-wake-lock

# Create a persistent notification
termux-notification --id gps_tracker --ongoing --priority high --title "GPS Tracker" --content "Starting GPS tracking..."

SESSION_DIR=~/record_gps-`date +%s`
mkdir ${SESSION_DIR}

counter=0
while true; do
    loc_json=`termux-location`

    if [ `echo "${loc_json}" | grep -c latitude` -ne 0 ]; then
        ts=`date +%s`;
        echo "${loc_json}" > ${SESSION_DIR}/${ts}.json;
        # Update notification every 10 iterations (every 30 seconds) to show it's alive
        counter=$((counter+1))
        if [ $((counter % 10)) -eq 0 ]; then
            termux-notification --id gps_tracker --ongoing --priority high --title "GPS Tracker" --content "Tracking GPS at $(date +%H:%M:%S)"
        fi
    fi

    sleep 3;
done

# Remove notification when script exits (if it ever does)
termux-notification-remove gps_tracker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment