I am using the HTML 5 Geolocation code (http://www.w3schools.com/html/html5_geolocation.asp) to get the users location.
...
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
I want to create a variable in ruby that holds the value of position.coords.latitude and position.coords.longitude, does anyone know the most effective way to capture this information and save it as a ruby variable?
-
2Use Ajax? JS executes on the client; Ruby on the server. How do you want to use the value in Ruby?Dave Newton– Dave Newton2013年09月04日 22:44:39 +00:00Commented Sep 4, 2013 at 22:44
-
Are you looking to manipulate this on the server side? Save it to a DB? Is this for a rails app?Beartech– Beartech2013年09月04日 22:47:00 +00:00Commented Sep 4, 2013 at 22:47
-
I'm ultimately looking to compare the user's location to various locations saved in my database. I imagine the best way to accomplish this is by saving the values in a params hash, but I'm not entirely sureReuben– Reuben2013年09月04日 22:55:29 +00:00Commented Sep 4, 2013 at 22:55
-
In order to save a value in a params hash it has to be submitted either via Ajax or a normal user-initiated request. Without knowing what you're really doing it's difficult to answer, but any standard HTTP mechanism will work.Dave Newton– Dave Newton2013年09月05日 02:23:28 +00:00Commented Sep 5, 2013 at 2:23
3 Answers 3
Well there are 2 ways you can do this -
Make ajax call
var latitude = position.coords.latitude; var longitude = position.coords.latitude; $.ajax({ url: "/<some_route>?latitude="+latitude+"&longitude="+longitude, success: function(data) { // do something here like for example replace some arbitrary container's html with the stuff returned by server. $("#container").html(data); } });Access it on the server side using params hash
latitude = params[:latitude] longitude = params[:longitude]Set a location cookie on the client side and access it on the server side.
var latitude = position.coords.latitude; var longitude = position.coords.longitude; document.cookie = "cl=" + latitude + "&" + longitude + ";";And in your controller you can access this -
current_location = cookies[:cl] unless current_location.nil? latitude = current_location.split('&')[0] longitude = current_location.split('&')[1] end
Having said that you should prefer option 1 because cookies are sent to the server with each HTTP request and additionally you might need to inform users that tracking technologies are used.
1 Comment
A request have, on high, the following steps:
- Browser requests a page to the server
- The server processes the request and gives back the page, which might include Javascript code
- The browser executes the javascript code and keep working on it
The question of how to have a variable that is only present in 3 on step 2, this is impossible.
An alternative is simply add an ajax call (that is, repeat steps 1 to 3, but instead of "Browser" read "Javascript code") that sends this information to the server to save in the database or something, but you will still have to program your logic in javascript.
Comments
does anyone know the most effective way to capture this information and save it as a ruby variable?
What is your measurement of effective? The data won't be missing a random digit when it gets to the server?
Making a jQuery ajax request is easiest:
var lat = 10;
var lon = 20;
function onsuccess(data, textStatus, jqXHR) {
alert(textStatus);
}
$.post(
"http://localhost:3000/some/route",
{'lat': lat, 'lon': lon},
onsuccess //optional
);
The latitude and longitude can be retrieved inside your action from the params hash:
lat = params[:lat]
lon = params[:lon]