Skip to content

Instantly share code, notes, and snippets.

@cclauss
Last active December 23, 2015 04:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cclauss/6578926 to your computer and use it in GitHub Desktop.
Save cclauss/6578926 to your computer and use it in GitHub Desktop.
The GPS issue was fixed quite elegantly with the **location module** added in Pythonista v1.4. The code below is not longer required for Pythonista. gpsForPythonista using BaseHTTPServer and XMLHttpRequest Thanks to 'Hvmhvm' of Pythonisa fourm for working code Thanks to 'Anton2' of Pythonisa fourm for XMLHttpRequest
# The GPS issue was fixed quite elegantly with the location module
# added in Pythonista v1.4. The code below is not longer required.
# gpsForPythonista using BaseHTTPServer and XMLHttpRequest
# Source at https://gist.github.com/cclauss/6578926
# Thanks to 'Hvmhvm' of Pythonisa fourm for working code
# Thanks to 'Anton2' of Pythonisa fourm for XMLHttpRequest
# http://omz-software.com/pythonista/forums/discussion/92/gps-access-solved
# usage: from gpsForPythonista import getGPS; print(getGPS())
# or from gpsForPythonista import getLatLong; print(getLatLong())
# or from gpsForPythonista import getGPS; print(getGPS().altitude)
import BaseHTTPServer, cgi, webbrowser
from collections import namedtuple
GPSfields = ('latitude', 'longitude', 'altitude', 'accuracy',
'altitudeAccuracy', 'heading', 'speed', 'timestamp')
GPSvalues = namedtuple('GPSvalues', GPSfields)
gGPSvalues = None # Global variable that will hold GPS info
HTML = """<!DOCTYPE html>
<html>
<body>
<div style="text-align:center;">
<H1>Gathering GPS data... Tap the Done button to see the results...</H1>
<H2>Please ensure that Localization Services are enabled for Pythonista:</H2>
<H3>Settings app --> Privacy --> Localization Services --> Pythonista (ON)</H3>
</div>
<script>
function returnToPythonista()
{
window.open('pythonista://', '_self', '');
window.setTimeout(function () {
window.close();
}, 100);
}
function sendPosition(position)
{
var xhr = new XMLHttpRequest();
xhr.addEventListener("loadend", function () {
window.setTimeout(returnToPythonista, 100);
}, false);
xhr.open('POST', '', true);
xhr.send(position.coords.latitude + '&'
+ position.coords.longitude + '&'
+ position.coords.altitude + '&'
+ position.coords.accuracy + '&'
+ position.coords.altitudeAccuracy + '&'
+ position.coords.heading + '&'
+ position.coords.speed + '&'
+ new Date(position.timestamp).toLocaleString())
}
navigator.geolocation.getCurrentPosition(sendPosition);
</script>
</body>
</html>"""
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def sendHeaders(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
def do_GET(self): # load initial page
self.sendHeaders()
self.wfile.write(HTML)
def do_POST(self): # process request
contentLength = int(self.headers.getheader('content-length'))
postBody = self.rfile.read(contentLength)
global gGPSvalues
gGPSvalues = GPSvalues(*postBody.split('&'))
def getGPS():
global gGPSvalues
gGPSvalues = None
httpd = BaseHTTPServer.HTTPServer(('', 80), RequestHandler)
webbrowser.open('http://localhost') # try both http:// and safari-http://
while not gGPSvalues:
httpd.handle_request()
return gGPSvalues
def getLatLong():
GPSvalues = getGPS()
return (float(GPSvalues.latitude),
float(GPSvalues.longitude))
if __name__ == '__main__':
#print(getLatLong()) # safari-http:// stops the script here
print(getGPS())
print(getGPS().altitude)
@Anton-2
Copy link

Anton-2 commented Sep 16, 2013

Self closing version : https://gist.github.com/Anton-2/6587375
I'm going to add the other location informations.

@cclauss
Copy link
Author

cclauss commented Sep 17, 2013

Retooled to use Anton-2's excellent insights about XMLHttpRequest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment