Skip to content

Instantly share code, notes, and snippets.

@mattspence
Created October 2, 2013 23:31
Show Gist options
  • Save mattspence/6802102 to your computer and use it in GitHub Desktop.
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
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