Displaying a polygon with gmaps4rails

痴心易碎 提交于 2019-11-30 16:03:17

I passed their base tutorial (from screen cast) and markers works fine. But I had problem with polylines (as you with polygons). At the end I resolved my problem (it can help you with polygons). So, view is the same as they gave :

<%= gmaps({
       "polylines"    => { "data" => @bemap_polylines }
     })
 %>

The model is also the same as their.

class Character < ActiveRecord::Base
  belongs_to :bemap
  acts_as_gmappable

  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
    address
    logger.info address
  end

end

So, the main problem was into controller. Here is mine :

  def show
    @bemap = Bemap.find(params[:id])
    @bemap_polylines = []
    @bemap_characters = []
    @bemap.characters.each do |v|
      @bemap_characters << { :lng => v[:longitude], :lat => v[:latitude]}
    end
    @bemap_polylines << @bemap_characters
    @bemap_polylines =  @bemap_polylines.to_json
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @bemap }
    end
  end

Gmaps4rails doesn't provide any builtin method to create the js for polygons.

The one you use is malformed, see doc: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Polygons.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!