I am working on a web app where I want to get user's current geolocation coordinates (Latitude. Longitude, Altitude). I am using Ruby on Rails with MongoDB (mongoid). I am getting users coordinate through HTML5 and Javascript.
for now I am displaying those coordinate in users/new.html.erb where I lodad a script in the head of new.html.erb such as
<!DOCTYPE html>
<html>
<head>
<script>
function getUserLocation() {
//check if the geolocation object is supported, if so get position
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(displayLocation, displayError);
else
document.getElementById("locationData").innerHTML = "Sorry - your browser doesn't support geolocation!";
}
function displayLocation(position) {
//build text string including co-ordinate data passed in parameter
var displayText = "User latitude is " + position.coords.latitude + " longitude is " + position.coords.longitude + " and altitude is " + position.coords.altitude;
var loc = [[position.coords.latitude, position.coords.longitude][position.coords.altitude]]
//display the string for demonstration
document.getElementById("locationData").innerHTML = displayText;
}
</script>
</head>
and in body tag I am requesting user to click on a button to get the cordinate display.
<input type="button" value="get location" onclick="getUserLocation()"/>
<div id="locationData">
Location Data
</div>
what I want to do is, transfer this location data (var loc) to my model places. The mongoid model is like below
model/place.rb
class Place
include Mongoid::Document
belongs_to :user
embeds_one :location
index({location: "2dsphere"})
end
model/location.rb
class Location
include Mongoid::Document
field :type, type: String
field :coordinates, type: Array
embedded_in :place
end
I want to know how can I pass javascript variable as a parameters to controller#new method and then to my model place where location is an embedded document.
Can someone please help with this?