Created
October 2, 2013 23:31
-
-
Save mattspence/6802102 to your computer and use it in GitHub Desktop.
Geolocate the browser and store lat,lng in session if and only if it's not already in session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
controller: | |
def index() | |
return dict(latitude=session.latitude, longitude=session.longitude) | |
def set_location(): | |
session.latitude = request.vars.lat | |
session.longitude = request.vars.lng | |
default/index.html: | |
{{extend 'layout.html'}} | |
{{if not session.latitude:}} | |
<script> | |
if(navigator.geolocation) | |
{ | |
navigator.geolocation.getCurrentPosition(postLocation); | |
function postLocation(position) | |
{ | |
jQuery.post( '{{=URL('set_location')}}',{lat: +position.coords.latitude, lng: +position.coords.longitude }); | |
} | |
} | |
</script> | |
{{pass}} | |
{{if 'latitude' in globals():}} | |
<p> | |
Lat: {{=latitude}} | |
</p> | |
<p> | |
Lng: {{=longitude}} | |
</p> | |
{{pass}} | |
{{=response.toolbar()}} | |
{{if 'message' in globals():}} | |
<h3>{{=message}}</h3> | |
{{elif 'content' in globals():}} | |
{{=content}} | |
{{else:}} | |
{{=BEAUTIFY(response._vars)}} | |
{{pass}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment