0

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?

1 Answer 1

0

Got it resolved. Thanks to This answer

used the below code in script

 $.post('/places/create',{lat: position.coords.latitude, 
                            lng: position.coords.longitude, 
                            alt:position.coords.altitude });

and in controller

  def create
      @lng_lat = [params[:lng], params[:lat]]
      @alt = params[:alt]
      @place = Place.create(location:{type:"Point", coordinates:@lng_lat}, height:@alt)
    end

But, As I was using geospatial feature of MongoDB(Mongoid). After indexing coordinates "2dsphere". I was getting error: "failed with error 16755: "insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?:" This error occurs when we have "2dsphere" indexing for coordinates.

Then I changed params to float in controller before passing it to model, as i think cordinates from javascript where being passed as string. so changed the controller code to

 @lng_lat = [params[:lng].to_f, params[:lat].to_f]
  @alt = params[:alt].to_f

It worked perfectly fine.

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

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.