当鼠标悬停在标记上时,我希望出现 infowindow,这是我在咖啡脚本中的代码:
$(document).on 'map:ready', -> addHoverHandlers()
addHoverHandlers = ->
# m is Gmap4Rails marker, doc in gmaps4rails.base.js.coffee
for m in Gmaps.map.markers
# marker is a Google Maps Marker
# https://developers.google.com/maps/documentation/javascript/reference#Marker
marker = m.serviceObject
console.log marker.getPosition().toString()
# Show the infowindow when user mouses-in
google.maps.event.addListener marker, "mouseover", ->
console.log marker.getPosition().toString()
m.infowindow.open marker.map, marker
# Hide the infowindow when user mouses-out
google.maps.event.addListener marker, "mouseout", ->
m.infowindow.close()
此代码在加载时输出:
(39.7317, -104.92099999999999)
(35.2638, -118.91200000000003)
(36.6624, -121.64499999999998)
但这在鼠标悬停在每个标记上时(坐标不变):
(36.6624, -121.64499999999998)
为了在上下文中提出问题,这是我map:ready
在 erb 文件中触发事件的方式:
<% content_for :scripts do %>
<script type="text/javascript">
Gmaps.map.callback = function(){
console.log('callback');
$(document).trigger('map:ready');
}
</script>
<% end %>
<%= yield :scripts %>