2013-08-26 58 views
1

也许这是星期一的情况,但我对infowindows和Google Maps for Rails gem感到非常困难。有谁知道一个教程或例子?Google Maps for Rails信息窗口示例

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

谢谢!

回答

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 

show view(haml)

#map-column 
    %h1 
    Find a retailer near you 

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