Skip to content

Commit

Permalink
Function and examples ( http://0.0.0.0:8000/webapp/geolocation ) to g…
Browse files Browse the repository at this point in the history
…eolocation.

$.pos() ask for geolocation just the first time, and save result for the next time.
$.pos() admit two function as parameters, one to success, and another to fail.
  • Loading branch information
s-nt-s committed Sep 24, 2016
1 parent a473205 commit 24e6a22
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
27 changes: 27 additions & 0 deletions semillas_backend/static/js/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ $('a.back-to-top').click(function() {
}, 700);
return false;
});

/* Get location */

$.pos=function(showPosition,showError){
if (!this._pos) {
if (!navigator.geolocation) {
this._pos={UNSUPPORTED:true}
if (showError) showError(this._pos);
} else {
this._pos={ASKING: true};
var _showPosition=function(position) {
$._pos=position;
if (showPosition) showPosition.apply(this,arguments);
};
var _showError=function(error) {
$._pos=error;
if (showError) showError.apply(this,arguments);
};
navigator.geolocation.getCurrentPosition(_showPosition, _showError);
}
} else {
if (showPosition && this._pos.coords) showPosition(this._pos);
else if (showError) showError(this._pos)
}
return this._pos;
};

49 changes: 49 additions & 0 deletions webapp/static/webapp/js/geoexample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function reset() {
$._pos=null;
}
function resetMap () {
document.getElementById("mapholder").innerHTML = "";
}
function getLocation() {
$.pos(
function(position){
var latlon = position.coords.latitude + "," + position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="+latlon+"&zoom=14&size=400x300&sensor=false";

This comment has been minimized.

Copy link
@iesteban

iesteban Sep 24, 2016

Collaborator

I think it's not working in https://alpha.semillasocial.org/webapp/geolocation because is mixing htttps and http.

This comment has been minimized.

Copy link
@s-nt-s

s-nt-s Sep 24, 2016

Author Contributor

Can give me more details? I have tested it in windows xp with firefox and work.

This comment has been minimized.

Copy link
@iesteban

iesteban Sep 25, 2016

Collaborator

Yes, it's working in my phone. Sorry.

It could be because of Linux Mint then.
I am getting this in my console.

selection_003

And I thought it was because of this line in geolocation.js:
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="+latlon+"&zoom=14&size=400x300&sensor=false";

But I wouldn't care since seems to be working perfectly. Sorry for the false positive.

document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
},
function(error){
var x = document.getElementById("demo");
if (error.UNSUPPORTED) {
x.innerHTML = "Geolocation is not supported by this browser.";
return;
}
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred.";
break;
}
}
);
}
function getLocation2() {
alert(JSON.stringify($.pos()));
}

function getLocation3() {
$.pos(
function() {
alert("OK");
},function() {
alert("KO");
}
);
}
22 changes: 22 additions & 0 deletions webapp/templates/webapp/geolocation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}
{% load static %}

{% block javascript_level2 %}
<script type="text/javascript" src="{% static 'webapp/js/geoexample.js' %}" defer="defer"></script>
{% endblock %}

{% block content %}



<p id="demo">Click the button to get your position.</p>

<button onclick="getLocation()">Set Map</button>
<button onclick="resetMap()">Clean Map</button>

<div id="mapholder"></div>

<button onclick="getLocation2()">Example 2</button>
<button onclick="getLocation3()">Example 3</button>

{% endblock %}
7 changes: 6 additions & 1 deletion webapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

urlpatterns = [
url(
regex=r'',
regex=r'^$',
view=TemplateView.as_view(template_name='webapp/index.html'),
name='webapp'
),
url(
regex=r'^geolocation$',
view=TemplateView.as_view(template_name='webapp/geolocation.html'),
name='geolocation'
),
]

0 comments on commit 24e6a22

Please sign in to comment.