Google Maps for Rails信息窗口示例

时间:2013-08-26 19:59:34

标签: gmaps4rails

也许这是星期一的情况,但我在使用infowindows和Google Maps for Rails宝石时非常困难。有谁知道教程或示例?

我想要做的就是设置一个默认的信息窗口,当你点击一个标记时打开它。我已经收集到了我需要制作一个部分并在地图中设置选项,但我似乎无法将它们全部整合在一起。

谢谢!

1 个答案:

答案 0 :(得分: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

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

显示视图(haml)

#map-column
  %h1 
    Find a retailer near you

  = gmaps("markers" => {"data" => @json, "options" => {"link_container" => "map_link_" } })