开发者

How to add link to marker using gmaps4rails gem

开发者 https://www.devze.com 2023-03-24 00:27 出处:网络
I would like to add a marker with link in marker, so, when i click that marker, i will redirect to another page

I would like to add a marker with link in marker, so, when i click that marker, i will redirect to another page

Is anybody know to do it?

Thanks


I already added my code like this :

In controller:

@json = @businesses.results.to_gmaps4rails do |object|
  "\"link\": \"#{root_url}\""
end

In view开发者_JAVA技巧 :

  <%= gmaps4rails(@json) %>
  <% content_for :scripts do %>
    <script type="text/javascript">
      function redirect_to(url) {
        window.location = url;
      };
      Gmaps4Rails.callback = function() {
        function say_yo(arg) { return function(){alert('yo '+ arg + '!' );};};
        for (var i = 0; i <  Gmaps4Rails.markers.length; ++i) {
          google.maps.event.addListener(Gmaps4Rails.markers[i].google_object, 'click', redirect_to(Gmaps4Rails.markers[i].link));
        }
      }
    </script>
  <% end %>

Is it any wrong? because there just an info window that show after i clicked the marker(Not redirect to any page)

How to add link to marker using gmaps4rails gem


First include the link inside the json:

Model.all.to_gmaps4rails do |object|
  "\"link\": \"your link as string\""
end

Then add the extra listeners in your view (beware to include this AFTER your call to the gmaps method):

<%= gmaps(whatever you need here) %>

<% content_for :scripts do %>
<script type="text/javascript">
function redirect_to(url) {
  window.location = url;
};
Gmaps4Rails.callback = function() { 
  function say_yo(arg) { return function(){alert('yo '+ arg + '!' );};};
    for (var i = 0; i <  Gmaps4Rails.markers.length; ++i) {
      google.maps.event.addListener(Gmaps4Rails.markers[i].google_object, 'click', redirect_to(Gmaps4Rails.markers[i].link));
    }
}
</script>
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号