"""A simple Python module to text your last known location to the telephone number specified in TO. 
Uses the Google Maps reverse geocoder to convert lat/lon to place names. 
tristan@roddis.org June 2009"""

import android
import json
import urllib2

FROM = 'YOUR NAME'
TO = 'RECIPIENT TELEPHONE NUMBER'

GEOCODER_URL = 'http://maps.google.com/maps/geo?'

droid = android.Android()

location = droid.getLastKnownLocation()['result']

url = GEOCODER_URL + 'q=%s,%s' % (location['latitude'], location['longitude'])
data = urllib2.urlopen(url).read()
result = json.loads(data, encoding='iso-8859-1')
#print result

if result['Status']['code'] == 200:
  place = result['Placemark'][0]['address']
else:
  place = 'an unknown location'

droid.sendTextMessage(TO, "%s is at %s" % (FROM, place))

droid.makeToast("Message sent to %s telling them you are at %s" % (TO, place) )
droid.exit()

