1

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!

4

1 回答 1

1

没关系,它终于点击了。这是我的基本示例代码,希望将来能对其他人有所帮助。

定位模型

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

位置控制器

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

信息窗口部分 (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

显示视图 (haml)

#map-column
  %h1 
    Find a retailer near you

  = gmaps("markers" => {"data" => @json, "options" => {"link_container" => "map_link_" } })
于 2013-08-26T20:10:58.657 回答