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

  • SEARCH
  • Home
  • 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 9248617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:59:04+00:00 2026-06-18T09:59:04+00:00

I am using Gmaps4rails and have successfully got quite a bit working, but I

  • 0

I am using Gmaps4rails and have successfully got quite a bit working, but I am a bit stumped. Probably due to my novice JS skills. I have been trying to follow examples in various questions but I am not sure where I am going wrong.

When my map loads the Gmaps.map.callback is working fine, Map is loaded, I click on a marker and the listener event fires.

In my view:

<% content_for :scripts do %>
<script type="text/javascript">
Gmaps.map.callback = function(){
  for(var i = 0; i < Gmaps.map.markers.length; i++){
    marker = Gmaps.map.markers[i];
    google.maps.event.addListener(marker.serviceObject, 'click', 
      (function(marker){
        return function(){      
      alert(marker.id);
        }
      })(marker)
   )
 }
}
</script>
<% end %>

This will alert with the marker id from the json created in the controller.

I then have a search function that updates the location of the map and replaces the markers. This is getting called in an ajax call. So in the controllers .js.erb I do this:

Gmaps.map.replaceMarkers(<%= raw @json %>);

Everything seems fine, my map is loaded with the new markers, but the even listener does not work on the markers.

So my question is, how do I get these new markers to have the Listener for the click event. Do I need to reset the callback somehow? I think it has to run before the map gets loaded but in this case I am not reloading the map, just replacing the markers.

  • 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-18T09:59:05+00:00Added an answer on June 18, 2026 at 9:59 am

    Extract your method to reuse it:

    Gmaps.map.listen_to_markers = function(markers){
      for(var i = 0; i < markers.length; i++){
        marker = markers[i];
        google.maps.event.addListener(marker.serviceObject, 'click', 
          (function(marker){
            return function(){      
              alert(marker.id);
            }
          })(marker)
       )
     }
    }
    
    Gmaps.map.callback = function(){
      Gmaps.map.listen_to_markers(Gmaps.map.markers);
    }
    

    And whenever you replace markers:

    var new_markers = <%= raw @json %>;
    Gmaps.map.replaceMarkers(new_markers);
    Gmaps.map.listen_to_markers(new_markers);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Gmaps4rails. It's working really fine. But now I got an issue
I'm using gmaps4rails on my project. This gem works amazing but i have a
I'm using gmaps4rails, and trying to develop some tests. I have a factory factory
I have been using google maps for a web app where we enter our
I'm using gmaps4rails and really enjoying it, but, I missed setting some properties, such
I am using gmaps4rails and I have a set of dynamic generated markers, about
I'm using gmaps4rails and I have a good amount of markers, now when I
I am using rails 3.2.3 and gmaps4rails gem. I have one model called contact.
I have been using gmaps4 rails to build out a site and the gem
I'm using gmaps4rails awesome gem in my current project and have to import more

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.