0

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?

4
  • 2
    Use Ajax? JS executes on the client; Ruby on the server. How do you want to use the value in Ruby? Commented 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? Commented 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 sure Commented 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. Commented Sep 5, 2013 at 2:23

3 Answers 3

2

Well there are 2 ways you can do this -

  1. 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]
    
  2. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I used your second strategy and its doing exactly what I need
1

A request have, on high, the following steps:

  1. Browser requests a page to the server
  2. The server processes the request and gives back the page, which might include Javascript code
  3. 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

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.