Rails Google Maps integration Javascript problem

Posted by JZ on Stack Overflow See other posts from Stack Overflow or by JZ
Published on 2010-04-06T05:47:51Z Indexed on 2010/04/06 5:53 UTC
Read the original article Hit count: 331

I'm working on Rails 3.0.0.beta2, following Advanced Rails Recipes "Recipe #32, Mark locations on a Google Map" and I hit a road block: I do not see a google map. My @adds view uses @adds.to_json to connect the google maps api with my model. My database contains "latitude" "longitude", as floating points. And the entire project can be accessed at github.

Can you see where I'm not connecting the to_json output with the javascript correctly? Can you see other glairing errors in my javascript? Thanks in advance!

My application.js file:

    function initialize() { 
      if (GBrowserIsCompatible() && typeof adds != 'undefined') {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.addControl(new GLargeMapControl());

        function createMarker(latlng, add) {
          var marker = new GMarker(latlng);
          var html="<strong>"+add.first_name+"</strong><br />"+add.address;
          GEvent.addListener(marker,"click", function() {
            map.openInfoWindowHtml(latlng, html);
          });
          return marker;
        }

        var bounds = new GLatLngBounds;
        for (var i = 0; i < adds.length; i++) {
          var latlng=new GLatLng(adds[i].latitude,adds[i].longitude)
          bounds.extend(latlng);
          map.addOverlay(createMarker(latlng, adds[i]));
        }
        map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
      }
    }  
    window.onload=initialize;
    window.onunload=GUnload;

Layouts/adds.html.erb:

  <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAeH4ThRuftWNHlwYdvcK1QBTJQa0g3IQ9GZqIMmInSLzwtGDKaBQvZChl_y5OHf0juslJRNx7TbxK3Q" type="text/javascript"></script>

<% if @adds -%>
<script type="text/javascript">
    var maps = <%= @adds.to_json %>;
</script>
<% end -%>

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about JavaScript