【发布时间】:2014-12-13 12:15:02
【问题描述】:
我使用 google maps api v3、gmaps4rails 和 infobox,但我不知道如何删除事件,即一旦点击标记就自动平移地图...
标记是从我的控制器发送的:
Gmaps4rails.build_markers(experiences) do |experience, marker|
marker.lat experience.latitude
marker.lng experience.longitude
marker.infowindow render_to_string(partial: "/trip_experiences/infowindow.html.erb", locals: {
experience: experience,
trip: trip
})
marker.title experience.name
end
我的地图是用 js 构建的,标记是通过在处理程序上调用 addMarkers 创建的:
handler = Gmaps.build('Google', { builders: { Marker: InfoBoxBuilder} }); handler.buildMap({ provider: mapOptions, internal: { id: 'map' } }, function(){ $.get(url, function(data) { handler.removeMarkers(markers); markers = handler.addMarkers(data); setCarouselOnInfowindow(); handler.bounds.extendWith(markers); callback(false) }); });
到目前为止,我已经尝试在地图选项中使用diasbleAutoPan:true,将标记设置为不可点击,然后在点击时添加一个侦听器,并进行了大量研究,但我没有找到类似的东西......所以我猜我做错了什么,但找不到什么!
任何帮助将不胜感激......许多tahnks
编辑:
正如@apneadiving 所建议的那样,我尝试在自定义构建器中覆盖 infowindow_binding 方法以删除@markers.panTo 行,但单击地图时地图仍会自动居中于标记上...。
这是自定义构建器的代码:
`
class @InfoBoxBuilder extends Gmaps.Google.Builders.Marker # inherit from base builder
# override method
create_infowindow: ->
return null unless _.isString @args.infowindow
boxText = document.createElement("div")
boxText.setAttribute("class", 'infobox-container') #to customize
boxText.innerHTML = @args.infowindow
@infowindow = new InfoBox(@infobox(boxText))
infobox: (boxText)->
content: boxText
,disableAutoPan: true
,pixelOffset: new google.maps.Size(-140, -40)
,alignBottom: true
,zIndex: null
,disableAutoPan: true
,closeBoxURL: ""
,boxStyle: {
width: "280px"
,opacity: 1
}
,infoBoxClearance: new google.maps.Size(100, 1000)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
infowindow_binding: =>
@constructor.CURRENT_INFOWINDOW.close() if @_should_close_infowindow()
@infowindow ?= @create_infowindow()
return unless @infowindow?
@infowindow.open( @getServiceObject().getMap(), @getServiceObject())
@marker.infowindow ?= @infowindow
@constructor.CURRENT_INFOWINDOW = @infowindow
` 任何帮助都会很棒....非常感谢您
【问题讨论】:
-
我不知道在哪里设置它(我对 ruby on rails 不熟悉),但你必须设置 infowindow 的选项
-
它在此处的构建器中处理:github.com/apneadiving/Google-Maps-for-Rails/blob/master/vendor/… 因为您有自定义构建器,您可以覆盖该函数并删除此行
-
非常感谢您的反馈!我将尝试覆盖该方法,而我在 js/coffee 中一无所知(同时,我传递数据以从控制器以 json 格式构建信息框,并直接在 js 中的视图中创建信息框......不是非常优化...)@apneadiving,非常感谢您的宝石,它非常简单。干得好!
-
嘿Appartement Dupleix,你有这个工作吗?我正在尝试做类似的事情,我能够通过复制整个引用的文件(marker.coffee)并将其用作 infoboxbuilder 类,然后我注释掉了有问题的行。
-
嗨@kindofgreat,不幸的是我没有按照apneadiving的说明(这是为了覆盖该功能)做到这一点......自动平移仍在工作......什么已经奏效,是创造我自己的信息框直接在视图中......但如果你最终破解了这个问题,我会愿意更新:)
标签: javascript google-maps google-maps-api-3 gmaps4rails