user5178035
user5178035

Reputation:

google map API simple markers

I have an application which uses the google map API. I am trying to display simple markers on the map. The markers are items in my database (including the latitude and the longitude values). I can display one marker on the map but I am not able to populate the map with all the items from the database.

I am only able to make calls to database to each individual item. Any suggestion would be appreciated.

My helper file:

def name(x)
    @name = Place.find(x).name
    return @name
end

def latitude(x)
    @latitude = Place.find(x).latitude
    return @latitude
end

def longitude(x)
    @longitude = Place.find(x).longitude
    return @longitude
end

This the variable I created for the map:

...

var myLatlng = new google.maps.LatLng(<%= latitude(1) %>, <%= longitude(1)%>)

...

            var marker1 = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: "<%= name(1) %>"
            });

...

Upvotes: 1

Views: 490

Answers (1)

Nitesh Choudhary
Nitesh Choudhary

Reputation: 46

this is html.haml syntax-- initialize a js array on your layout like

window.markers = [];

then on your view page-

@places = Place.all
@places.each do |place|
#javascript code
  $(document).ready(function(){      
    window.markers.push([ new google.maps.LatLng(#{place.latitude},#
    {place.longitude}), "#{place.name}"]);
  });
end

js code on view

$.each(window.markers,function(i,e){
  var marker = new google.maps.Marker({
  position: e[0],
  title: e[1],            
  map: map
  });
});

Upvotes: 1

Related Questions