Let others to locate you in the world using Android, Python, PHP and Google Maps

“Locate mii” – a DIY solution to publish your location to the web so that other people can locate “where in the world you are !!”

To locate me now, go to this URL:
http://goo.gl/QThKv

Requirements:

1 .An Android Phone with Android scripting environment (for Python support).

2. A GPRS connection to push your latitude and longitude values to the web server

3. A website where you can run PHP scripts (Otherwise, you can use the Local wireless setup with Internet connection)

Design:

1. A Python script reads the latitude and longitude values from the Android’s GPS and pushes it to the web server using GPRS

2. A PHP script in the web server to parse and write the submitted values to corresponding files in the server

3. Another PHP files to read those latitude and longitude values from server and pass it to the Google Maps api to help others locate you in this world

Code :

1. livegps.py ( To fetch and submit latitude and longitude to the web from an android phone)

Available at : http://pastebin.com/HusGn77z

==========================================================

”’
Started on 10, Dec 2010 by Maxin B. John <maxinbjohn@gmail.com>
This file is licensed under the GPLv3
”’

import android
import urllib2
import time
import string

droid = android.Android()

def get_update_gps_status():
”’ GET and Update the GPS status in Server”’

# PHP script expects the longitude and latitude in the URL as given below
GPS_URL=’http://www.maxinbjohn.info/locate-mii/test.php?lat=%s&long=%s’
# begins the gps location process in Phone
droid.startLocating()
# giving some time for the phone to think :)
time.sleep(10)

try:
# read the latitude and longitude from the phone
loc= droid.readLocation()[1]
# check for Null in pythonic way
if loc:
longitude = loc['network']['longitude']
latitude =  loc['network']['latitude']
# prepares the URL as expected by the PHP script
url = GPS_URL % (latitude, longitude)
print url
try:
# submits the latitude and longitude values to the web page
handler = urllib2.urlopen(url)
# to ensure the data submission
data = handler.read()
except URLError:
print “Error in Connection!”
finally:
handler.close()
except:
print “Error in getting the lattitde and longitude values!”

if __name__ == ‘__main__’:
”’ Updating the GPS status in Server ”’
while True:
get_update_gps_status()

=======================================================

2. test.php (sorry for the horrible naming.. It’s a simple PHP script to write down the latitude and longitude values)

Available at:  http://pastebin.com/A71D2ka9

=========================================

<html>
<?
$latitude = $_GET['lat'];
$longitude = $_GET['long'];
$latf = “mylattitude.txt”;
$longf = “mylongitude.txt”;
$latfh = fopen($latf, ‘w’) or die(“can’t open file”);
fwrite($latfh, $latitude);
fclose($latfh);
$longfh = fopen($longf, ‘w’) or die(“can’t open file”);
fwrite($longfh, $longitude);
fclose($longfh);
echo “var mylocation = new google.maps.LatLng($latitude, $longitude);”;
?>
</html>

=========================================

3. locate-mii.php

Available here: http://pastebin.com/89kpCGtp

============================================

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”initial-scale=1.0, user-scalable=no” />
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″/>

<title>Locate Mii</title>
<link href=”http://code.google.com/apis/maps/documentation/javascript/examples/default.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript” src=”http://maps.google.com/maps/api/js?sensor=true”></script>
<script type=”text/javascript”>

var initialLocation;
<?
$filename1 = “./mylattitude.txt”;
$handle1 = fopen($filename1, “r”) or die(“can’t open file”);
$lattitude = fread($handle1, filesize($filename1));
fclose($handle1);

$filename2 = “./mylongitude.txt”;
$handle2 = fopen($filename2, “r”) or die(“can’t open file”);
$longitude = fread($handle2, filesize($filename2));
fclose($handle2);
?>
var mylocation = new google.maps.LatLng(<? echo $lattitude ; ?>,<? echo $longitude; ?>);
var browserSupportFlag =  new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();

function initialize() {
var myOptions = {
zoom: 18,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById(“map_canvas”), myOptions);

browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}

function handleNoGeolocation(errorFlag) {
initialLocation = mylocation;
contentString = “I am here!”;
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}
</script>
</head>
<body onload=”initialize()”>
<div id=”map_canvas”></div>
</body>
</html>

==========================================

Todo:

1.   Code cleanup

2 .  Auto refreshing of google maps page

Warning:

It’s a DIY thing. I haven’t incorporated any security measures to the code. Code is really ugly and logic is pretty simple.

Finally don’t blame me if your boss locates you in a Cinema hall while you are in “Sick leave” :)

Comments are welcome !!


6 Responses to “Let others to locate you in the world using Android, Python, PHP and Google Maps”

Leave a Reply

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes