Search code examples
ruby-on-railsruby-on-rails-4gmaps4railsgmaps4rails2

combine variable in controller


Use gmaps4rails for map function on site:

The idea is to show on the map the marker of the house plus multiply sights.

I have this code;

controller:

@house = House.friendly.find(params[:id])
@location = @house.location
@sights_markers = Location.where("category = 'sights'")

records = [@location, @sights_markers].compact

@hash = Gmaps4rails.build_markers(records) do |location, marker|
  marker.lat location.latitude
  marker.lng location.longitude

end

view:

<div id="map" style='width: 100%; height: 500px; margin-top: 10px; margin-bottom: 20px;' ></div>

<script type=text/javascript> 

    handler = Gmaps.build('Google');
    handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers(<%=raw @hash.to_json %>);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();


    });
</script>

I get a the error message..no method found "latitude"

What am i doing wrong?


Solution

  • The point is to have an array. So, replace:

    records = [@location, @sights_markers].compact
    

    with

    records = @sights_markers.to_a + [@location]