Skip to content

Instantly share code, notes, and snippets.

@mbohun
Last active August 2, 2026 21:27
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"
}

visual test 👀

REFERENCES

#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
running=true;
trap 'running=false;' 2
SESSION_DIR=~/record_gps-`date +%s`
mkdir ${SESSION_DIR}
while ${running}; 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
echo "DEBUG: finnished";
exit 0;
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbohun

mbohun commented Nov 28, 2025

Copy link
Copy Markdown
Author

1000014759

@mbohun

mbohun commented Nov 29, 2025 •

Copy link
Copy Markdown
Author

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

mbohun commented Nov 29, 2025

Copy link
Copy Markdown
Author

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

mbohun commented Dec 2, 2025

Copy link
Copy Markdown
Author

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

@mbohun

mbohun commented Dec 13, 2025 •

Copy link
Copy Markdown
Author
cat record_gps-1765558485.geojson \
    | jq '[ .features[].properties.timestamp | strflocaltime("%Y-%m-%d %H:%M:%S") ]'
[
  "2025-12-12 17:54:46",
  "2025-12-12 17:54:50",
  "2025-12-12 17:54:55",
  "2025-12-12 17:54:59",
  "2025-12-12 17:55:03",
  "2025-12-12 17:55:07",
  "2025-12-12 17:55:11",
  "2025-12-12 17:55:15",
  "2025-12-12 17:55:19",
  "2025-12-12 17:55:23"
]

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