I have the following javascript code in rails 3.2.

function initialize() { var myLatLng = new google.maps.LatLng(34.553366482, 49.783977595); var mapOptions = { zoom: 12, center: myLatLng, mapTypeId: google.maps.MapTypeId.TERRAIN };

    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    var flightPlanCoordinates = [
        new google.maps.LatLng(34.553366482, 49.783977595),
        new google.maps.LatLng(34.554122088, 49.783895487),
        new google.maps.LatLng(34.554483305, 49.783853715),
        new google.maps.LatLng(34.555332372, 49.783720553),

        new google.maps.LatLng(34.556189752, 49.783658072),
        new google.maps.LatLng(34.556664709, 49.783605137),
        new google.maps.LatLng(34.556769403, 49.783602219),
        new google.maps.LatLng(34.556787772, 49.783469404),

        new google.maps.LatLng(34.556793727, 49.783317126),
        new google.maps.LatLng(34.555050604, 49.783479752),
        new google.maps.LatLng(34.554290633, 49.783555281),
        new google.maps.LatLng(34.55376046, 49.783620191),

        new google.maps.LatLng(34.553413552, 49.783659434),
        new google.maps.LatLng(34.553275583, 49.783705246),
        new google.maps.LatLng(34.553366482, 49.783977595),

    ];
    var flightPath = new google.maps.Polyline({
      path: flightPlanCoordinates,
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: 2
    });

    flightPath.setMap(map);
  }

</script>

this is a way o show points on the google maps. I wanna get the points from database and send to javascript. how can i do that in a loop?

share|improve this question
up vote 0 down vote accepted

You could create a javascript variable(json to be more specific) in one of your rails views so that it is available to your script above.

In controller/model:

@coordinates = Coordinates(..your query...).get_json

In your view where these points are going to show, depending or whether you use erb or haml, do:

Haml:

:javascript
 var coords = "#{@coordinates.to_json}"

Erb:

<script type="text/javascript">
  var coords = "<%= escape_javascript(@coordinates.to_json)%>"
</script>

Now you can iterate over the coords json in your flightPlanCoordinates above and make the markers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.