Search code examples
ruby-on-railsgoogle-mapsgmaps4railsgmaps4rails2

Rails: how to change a marker and add infowindow in Gmaps4rails


I'm using the Gmaps4rails gem and this is the setup I have:

def show
    @items = Item.find(params[:id])
    @hash = Gmaps4rails.build_markers(@items) do |item, marker|
      marker.lat item.latitude
      marker.lng item.longitude
      marker.picture({
                     :url => "http://maps.google.com/mapfiles/arrow.png",
                     :width   => 32,
                     :height  => 32
                 })
    end
    append_cur_location
  end

def append_cur_location
    @hash << { :lat=>action[0], :lng=>action[1]}
end

def action
  @lat_lng = cookies[:lat_lng].split("|")
end

I'm getting the current location from the action method and the item location from the item.latitude and item.longitude.

views/items/show.html.erb

<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

<script>
    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>

With the above setup I got the map to show the current location and the item location. Also, I was able to change the item location marker. But I cant figure out how to change the current location marker and add the info box to the item location. Any ideas how to implement these changes?


Solution

  • INFOWINDOW

    you can use marker.infowindow

    def show
      @items = Item.find(params[:id])
      @hash = Gmaps4rails.build_markers(@items) do |item, marker|
        marker.lat item.latitude
        marker.lng item.longitude
        marker.picture({
                       :url => "http://maps.google.com/mapfiles/arrow.png",
                       :width   => 32,
                       :height  => 32
                   })
    
        #INFOWINDOW
        # Use can use partial to render infowindow
        # marker.infowindow render_to_string(:partial => 'items/info_page')
        # or else
        marker.infowindow "ITEM HERE!!!!"
    
      end
      append_cur_location
    end
    

    DIFFERENT IMAGE FOR CURRENT LOCATION

    def append_cur_location
      @hash << { 
      :lat=>action[0], 
      :lng=>action[1],
      :picture=> {
          :url=> "http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32.png",
          :width=>  32,
          :height=> 32
        }
      }
    end