My requirement is to add a onclik listener on a marker. My view is like below
<div class="map-wrapper">
<div id="map_div" style=" height: 800px;"></div>
</div>
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<script>
handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
var mapOptions = { mapTypeId: google.maps.MapTypeId.SATELLITE };
handler.buildMap({ provider: mapOptions, internal: {id: 'map_div'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.getMap().setZoom(19);
});
</script>
<% 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 %>
But when looks like callback function is not getting invoked. Am i missing something here ,or in layout application.html.erb. In application.html.erb I had put
<%= yield %>
<%= yield :scripts %>
My controller code is like below
def index
@sens = Sen.all
@hash = Gmaps4rails.build_markers(@sens) do |sen, marker|
marker.lat sen.latitude
marker.lng sen.longitude
marker.picture({"url" =>"/images/red_bl.gif","width" => 36, "height" => 36 })
end logger.info @hash end
rails version is 4.0.2 ruby 1.9.3 'gmaps4rails', '2.1.1'
Gmaps.map.callback
is not an existing method in v2.x.
If you need a callback, simply add it after:
markers = handler.addMarkers(<%=raw @hash.to_json %>);
BTW, I recommend you use:
marker.getServiceObject()
<%= yield :scripts %>
is not necessary in v2.x