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.

Advertisements
Posted in Location to HTTP, Uncategorized
One comment on “Location to HTTP – Send GPS data to remote script
  1. […] launching Location to HTTP, I’ve been tinkering with additional data collection from my cellphone such as Bluetooth, […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,101 other followers

Instagram
Progress on the house... House foundation is complete.  Should be fast progress all summer. Why would you do that? Questionable home staging On top of the old @seattletimes building - "We want free wifi and $2 hoes"
#officeviews And from the other side...
Follow Pi Fun on WordPress.com
%d bloggers like this: