Location to HTTP – Send GPS data to remote script

I wanted to capture my phone’s location information and store it remotely (on my server) so I could do with it as I please.  Most of the apps I found were severely limited in that they only mapped within the app, were riddled with ads, crashed frequently, or were untrustworthy (they’re storing my location, after all).  So, I decided to build my own app that did what I wanted.

Location to HTTP

banner-edit
Get it on Google Play
Location to HTTP is an app I’m working on which allows users to input a remote script URL to capture a device’s location information via POST.

How it works

  1. Create a remote script to capture the variables $lat and $lon via POST (example below).
  2. Download the app and enter the URL of that script.
  3. The app will automatically send the location information every 5-10 minutes to the script.

Possible Uses

  • Track your phone’s location so you can retrace your steps if you lose it.
  • Store your location history for displaying in custom interfaces.
  • Share your location with friends/family when travelling or hiking.
  • Trigger home automation events based on your location (turn on the heat, turn on the lights, unlock the doors, feed the pets, water the plants, etc.).
  • Measure the amount of time you spend at specific locations.

Sample Script

The beauty (and utility) of this app is that you can do whatever you like with the output.  In my use case, I want to store my location history to track my travel history.  I do this using a simply PHP script and MySQL database.
Database Structure

CREATE TABLE IF NOT EXISTS `phone_location` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lat` varchar(200) NOT NULL,
`lon` varchar(200) NOT NULL,
`time` varchar(200) NOT NULL,
`address` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

PHP Script

<?
//Prevent caching and set content-type to json so app can display it correctly.
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/json');
//Pull address for latitude and longitude from Google API (max 25,000 calls per day for free)
function getaddress($lat,$lon) {
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lon);
$json = @file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status=="OK"){
return $data->results[0]->formatted_address;
} else {
return false;
}
}
//Set the response we want to display in the app
date_default_timezone_set('America/Los_Angeles');
$timestamp = date('Y-m-d h:i:s');
$success_response = "Successfully captured location @ $timestamp PST!";
//Connect to MySQL database
$conn = mysqli_connect("", "", "", "");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//Capture the latitude ($lat) and longitude ($lon) passed from the app
$lat=$_REQUEST['lat'];
$lon=$_REQUEST['lon'];
//Get the address from Google API
$address= getaddress($lat,$lon);
if(!$address){
$address= "Not found";
}
//Insert record into database
$sql = "INSERT INTO phone_location (lat, lon, time, address)
VALUES ('$lat', '$lon', '$timestamp', '$address')";
if (mysqli_query($conn, $sql)) {
echo json_encode($success_response);
} else {
echo json_encode("Error: Unable to create posting.");
}
mysqli_close($conn);
?>

Google API Integration for Address Data

As shown in the example above, you can easily get address information using the Google API and reverse geocoding.

One thought on “Location to HTTP – Send GPS data to remote script

Leave a Reply