Search code examples
ruby-on-railsrubyruby-on-rails-3gmaps4rails

Make map marker direct link onclick for gmaps4rails


Currently have the infowindow showing as the 'what happens' when the map marker is clicked, as so:

        google.maps.event.addListener(marker, 'click', function(){
            infowindow.open(Gmaps.map.map, marker);
        });

How would I get this to work to automatically link to the marker's SHOW page, ie. where is it possible to put in a code reference:

<a href='/controller/#{slug}'>#{title}</a> 

or

<%= link_to %> function

Solution

  • For this kind of needs, I pass a block to the gmaps4rails method in the controller (doc here):

    @json = User.all.to_gmaps4rails do |user, marker|
      marker.json "\"id\": #{user.id}"
      # or
      marker.json "\"link\": #{method_to_create_link}"
    end
    

    This way I can have any additional information I need to create a link or anything.

    That said, you could update your listener this way:

    base_url = "whatever you need";
    google.maps.event.addListener(marker, 'click', function(){
       window.location(base_url + marker.id);
       // or
       window.location(marker.link);
    });