Guest

livegps.py

By: a guest on Dec 11th, 2010  |  syntax: Python  |  size: 1.54 KB  |  hits: 298  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. '''
  2. Started on 10, Dec 2010 by Maxin B. John <maxinbjohn@gmail.com>
  3. This file is licensed under the GPLv3
  4. '''
  5.  
  6. import android
  7. import urllib2
  8. import time
  9. import string
  10.  
  11. droid = android.Android()
  12.  
  13. def get_update_gps_status():
  14.     ''' GET and Update the GPS status in Server'''
  15.    
  16.     # PHP script expects the longitude and latitude in the URL as given below
  17.     GPS_URL='http://www.maxinbjohn.info/locate-mii/test.php?lat=%s&long=%s'
  18.     # begins the gps location process in Phone
  19.     droid.startLocating()
  20.     # giving some time for the phone to think :)
  21.     time.sleep(10)
  22.  
  23.     try:
  24.         # read the latitude and longitude from the phone
  25.         loc= droid.readLocation()[1]
  26.         # check for Null in pythonic way  
  27.         if loc:
  28.             longitude = loc['network']['longitude']
  29.             latitude =  loc['network']['latitude']
  30.             # prepares the URL as expected by the PHP script
  31.             url = GPS_URL % (latitude, longitude)
  32.             print url
  33.             try:
  34.                 # submits the latitude and longitude values to the web page
  35.                 handler = urllib2.urlopen(url)
  36.                 # to ensure the data submission
  37.                 data = handler.read()
  38.             except URLError:
  39.                 print "Error in Connection!"
  40.             finally:
  41.                 handler.close()
  42.     except:
  43.         print "Error in getting the lattitde and longitude values!"
  44.  
  45. if __name__ == '__main__':
  46.     ''' Updating the GPS status in Server '''
  47.     while True:
  48.         get_update_gps_status()