标记没有显示使用地理编码器和gmaps4rails

时间:2015-03-14 18:40:26

标签: gmaps4rails rails-geocoder

我正在使用geocoder gem和gmaps4rails gem。我已按照安装两者的说明进行操作,并且我能够在页面上显示虚拟地图,但是,当我尝试从控制器添加@hash时,标记不会显示。任何帮助,将不胜感激!我是新手: - )

我的'显示'观点:

    <script type="text/javascript">
  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>

My Trail Controller:

def index
    @trails = Trail.all
    @hash = Gmaps4rails.build_markers(@trails) do |trail, marker|
      marker.lat trail.latitude
      marker.lng trail.longitude
      marker.title trail.title
    end
  end

  def show
    @trail = Trail.find(params[:id])
    @posts = @trail.posts
    @hash = Gmaps4rails.build_markers(@trails) do |trail, marker|
      marker.lat trail.latitude
      marker.lng trail.longitude
      marker.title trail.title
    end
  end

  def new
    @trail = Trail.new
  end

  def create
    @trail = Trail.new(params.require(:trail).permit(:title, :state, :address, :latitude, :longitude, :body, :images))
    if @trail.save
      flash[:notice] = "Trail was saved."
      redirect_to @trail
    else
      flash[:error] = "There was an error saving the trail. Please try again."
      render :new
    end
  end

My Trail Model:

class Trail < ActiveRecord::Base
  belongs_to :user
  has_many :posts
  mount_uploader :images, ImagesUploader

  geocoded_by :address
  after_validation :geocode
end

0 个答案:

没有答案