Displaying a polygon with gmaps4rails

前端 未结 2 710
说谎
说谎 2021-01-03 11:16

Ruby noob here. Trying to display a list of points as a polygon on a google map using the gmaps4rails gem (awesome gem by the way). Any suggestions based on code sample be

相关标签:
2条回答
  • 2021-01-03 11:33

    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.

    0 讨论(0)
  • 2021-01-03 11:39

    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
    
    0 讨论(0)
提交回复
热议问题