Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:42:29+00:00 2026-06-02T06:42:29+00:00

I have been using gmaps4 rails to build out a site and the gem

  • 0

I have been using gmaps4 rails to build out a site and the gem is a super addition to my project. I am having some trouble however, with nested models and polygons.

I too would like to do something like this:
Gmaps4rails custom info window with javascript content

Models:

  class Zone < ActiveRecord::Base
    acts_as_gmappable

    has_many :points, :as => :pointable
    accepts_nested_attributes_for :points

    def self.create_with_points(name, points)
      zone = Zone.create(:name => name)
      points.map { |point| zone.points.create(:latitude => point[0],
                                              :longitude => point[1]) }
      zone
    end

    def as_polygon_data
      points.collect { |point| { "lat" => point.latitude.to_f,
                                 "lng" => point.longitude.to_f,
                                 "strokeColor" => "#EBAC2A",
                                 "strokeOpacity" => 0.65,
                                 "strokeWeight" => 2,
                                 "fillColor" => "#606F81",
                                 "fillOpacity" => 0.4
                               } }
    end

    def self.all_as_polygon_data
      Zone.all.collect { |zone| zone.as_polygon_data }
    end
  end

  class Point < ActiveRecord::Base
    belongs_to :pointable, :polymorphic => :true
    acts_as_gmappable
  end

To invoke the Zone in the controller I have to call it with to_json instead of to_gmaps4rails:

@polygon_json = Zone.all_as_polygon_data.to_json

The polygons render on the map perfectly, but I can’t add an info window to them. I tried the click event callback in the above-mentioned post, but it did not work for me.

I would also like to know how I can get a handle to these polygons so I can hide them using a checkbox.

Thank you for your time. Hopefully I have provided enough information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:42:31+00:00Added an answer on June 2, 2026 at 6:42 am

    but it did not work for me is not a very useful statement.

    I can’t know what’s your problem: do you have any js error?

    You can use the whole google js api, the gem is not a brake, it’s just a wrapper.

    If you want to interact with polygons:

    • the data you pass in the view are stored in the array of objects: Gmaps.map.polygons

    • on page load, the google’s polygons are created and are stored in an object I call serviceObject.

    To sum up, Gmaps.map.polygons[0].serviceObject is a google polygon object you can interact with freely.

    To be sure the google’s object are created, insert your custom methods in the gmaps4rails js callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using TortoiseSVN for some time and I really like it. I
I have been using an API to do some work. This is how I
I have been using some Javascript to create a text field once an certain
I've been using the gmaps4rails gem for a while in rails 3.1 and it
I have been using json.NET successfully in projects for some time now without any
I have been using Bart Duncan's classic article to find out what indexes I
I have been using Alaxos CakePHP ACLplugin for my site, which is great. However,
I have been using HTML4 for quite some time. I've heard of HTML5 for
I have been using the examples from this site ( http://dojotoolkit.org/documentation/tutorials/1.6/datagrid/ ) to render
I have been using WPF for a new project which is far from done.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.