Note that there are some explanatory texts on larger screens.

plurals
  1. POgmaps4rails: How to implement a clickable link on a marker
    text
    copied!<p>Using Rails 3.2.13, ruby 1.9.3p374 and Gmaps4rails 1.5.6.</p> <p>I have been strugling with something that, at first, I thought should be fairly straight forward: I want to display a marker that, when clicked, will redirect to a particular path, loading that path in the same window.</p> <p>Gmaps4rails has no easy option for that in their <a href="https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Display-Everything%21" rel="nofollow noreferrer">description</a>. So I spent countless hours searching the web, and the two best hits I found where here in StackOverflow: <a href="https://stackoverflow.com/questions/6874077/how-to-add-link-to-marker-using-gmaps4rails-gem">this</a> smells like the right path, but <a href="https://stackoverflow.com/questions/8608602/make-map-marker-direct-link-onclick-for-gmaps4rails">this one</a> seems also useful.</p> <p>In the end, my controller looks like:</p> <pre><code>@gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker | marker.title house.reference marker.json({:link =&gt; polymorphic_url(house, :routing_type =&gt; :path)}) end </code></pre> <p>This is correctly (I think) generating the following in the processed html:</p> <pre><code>&lt;script src="//maps.google.com/maps/api/js?v=3.8&amp;amp;sensor=false&amp;amp;client=&amp;amp;key=&amp;amp;libraries=geometry&amp;amp;language=&amp;amp;hl=&amp;amp;region=" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; Gmaps.map = new Gmaps4RailsGoogle(); Gmaps.load_map = function() { Gmaps.map.map_options.auto_zoom = false; Gmaps.map.map_options.zoom = 3; Gmaps.map.initialize(); Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}]; Gmaps.map.create_markers(); Gmaps.map.adjustMapToBounds(); Gmaps.map.callback(); }; Gmaps.oldOnload = window.onload; window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); }; &lt;/script&gt; </code></pre> <p>Markers appear on the right places. But now, interpreting the described on that <a href="https://stackoverflow.com/questions/6874077/how-to-add-link-to-marker-using-gmaps4rails-gem">first relevant reference</a>, I type directly on view, making sure it will appear below all the other javascripts Gmaps4Rails dumps in the resulting html:</p> <pre><code>&lt;script type="text/javascript"&gt; function redirect_to(url) { window.location = url }; Gmaps.callback = function() { for (var i = 0; i &lt; Gmaps.map.markers.length; ++i) { google.maps.event.addListener(Gmaps.map.markers[i].google_object, 'click', redirect_to(Gmaps.map.markers[i].link)); } }; &lt;/script&gt; </code></pre> <p>But now, whenever I load the page, my Firefox javascript console will say:</p> <pre><code>TypeError: Gmaps[load_function_name] is not a function for (key in Gmaps) { value = Gmaps[key]; searchLoadIncluded = key.search(/load/); if (searchLoadIncluded === -1) { load_function_name = "load_" + key; _results.push(Gmaps[load_function_name]()); &lt;=== Points to this line } else { _results.push(void 0); } } </code></pre> <p>So, any ideas on what is wrong?</p> <p>Thanks in advance,</p> <p>Marco</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload