Search code examples
gmaps4rails

Google Maps for Rails info window example


Maybe it's a case of the Mondays but I'm having a really difficult time with infowindows and the Google Maps for Rails gem. Does anyone know of a tutorial or example?

All I want to do is set up a default infowindow to open when you click on a marker. I've gathered that I need to make a partial and set the options in the map but I just can't seem to bring it all together.

Thanks!


Solution

  • Nevermind, it finally clicked. Here's my code for a basic example, hopefully it will help someone else in the future.

    location model

    class Location < ActiveRecord::Base
      include Rails.application.routes.url_helpers
    
      default_scope order('locations.id ASC')
    
      acts_as_gmappable
    
      attr_accessible               :name,
                                    :address, 
                                    :city,
                                    :province,
                                    :postal_code,
                                    :country,
                                    :phone,
                                    :ext,
                                    :phone_alt,
                                    :ext_alt, 
                                    :latitude, 
                                    :longitude 
    
      geocoded_by                   :address
    
      validates_presence_of         :name
      validates_presence_of         :address
      validates_presence_of         :city
      validates_presence_of         :province
      validates_presence_of         :postal_code
      validates_presence_of         :country
    
    
      after_validation              :geocode, :if => :address_changed?
    
      def gmaps4rails_address
        #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
        "#{self.address}, #{self.city}, #{self.country}" 
      end                          
    end
    

    location controller

    class LocationsController < ApplicationController
    
      def show
        @location = Location.find(params[:id])
        @json = @location.to_gmaps4rails do |location, marker|
          marker.infowindow render_to_string(:partial => "/layouts/partials/infowindow", :locals => { :location => location})
        end
    
        respond_to do |format|
          format.html
        end
      end
    end
    

    infowindow partial (haml)

    .location-data{id: location.id}
      .location-name
        = location.name.capitalize
      .location-address
        = location.address.capitalize
      .location-city= location.city.capitalize
      .location-province
        = location.province.capitalize
      .location-postal-code
        = location.postal_code
      .location-country
        = location.country
      .location-phone
        = location.phone
      .location-extension
        = location.ext
      .location-alt-phone
        = location.phone_alt
      .location-alt-phone-extension
        = location.ext_alt
    

    show view (haml)

    #map-column
      %h1 
        Find a retailer near you
    
      = gmaps("markers" => {"data" => @json, "options" => {"link_container" => "map_link_" } })