0

I want to show some coordinates in a Google map, it works fine to get it from a text file but I don't know how to access to coordinates from database.

Here the code working with a text file :

<script type="text/javascript">

        var map;
        var markers = [];

        var mon_fichier = fichier_txt("file.txt");
        var elem = mon_fichier.split(',');
        var tHandleMajActors; // Handle timer majActeurs()

        function initialize() {
          var haightAshbury = new google.maps.LatLng(elem[0], elem[1]);
          var mapOptions = {
            zoom: 6,
            center: haightAshbury,
            mapTypeId: google.maps.MapTypeId.TERRAIN
            /*mapTypeId: google.maps.MapTypeId.SATELLITE*/
          };
          map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);

          //run realtime view
          RTrun();
        }

        //RT MENU MANAGEMENT
        function RTrun(){
            movePositionMarker();
        }

        //RT MARKER MANAGEMENT
        function movePositionMarker(){          //XXX timer
            var mon_fichier = fichier_txt("file.txt");
            var elem = mon_fichier.split(',');
            i=0;    
            while (i<elem.length) {
                var myPosition = new google.maps.LatLng(elem[i], elem[i+1]);
                i=i+2;
                addMarker(myPosition);
            }
            //timer: every 10 s
            tHandleMajActors = setTimeout( function(){
                movePositionMarker();
                }, 10000);
        }

        // Add a marker to the map and push to the array.
        function addMarker(location) {
          var image = 'images/flag.png';
          var marker = new google.maps.Marker({
            //animation: google.maps.Animation.DROP,
            position: location,
            map: map,
            icon: image
          });
          markers.push(marker);
        }

The table from which I want to take coordinates is called Coordinate and has :latitude, :longitude, :date, :time, :tracker_id

Thank you for your help !

1
  • Are you using jquery already? Commented Jun 10, 2014 at 13:21

1 Answer 1

3

You can't access the database directly with javascript as it's on a different machine (the server) from the browser. You need to call an action on your app which will return the data you want.

In this case, you want to get the data in json format, so the javascript can translate that straight into an actual javascript object.

You can decide which action should return the data. In this case, if it's the data for a single Coordinate object then it would make sense to have the CoordinatesController#show action. This should be set up like so:

def show
  @coordinate = Coordinate.find(params[:id])
  respond_to do |format|
    format.html #defaults to rendering the /view/coordinates/show template
    format.json { render json: @coordinate }
  end
end

So, that's the rails end sorted.

The next thing is to call the above action from your javascript. I'm going to do it using jquery since it's just easier.

$.ajax({
  url: "/coordinates/"+coordinate_id,
  dataType: "json",
  success:  function(data){ 
    //data is the data you got from the server, do something with it
  }
});

In this example you'll need to know the id of the coordinate object in order to pass it through.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.