javascript - 在( 谷歌地图 ) 上,在单击时不可能阻止自动平移到 marker

  显示原文与译文双语对照的内容
0 0

我使用 谷歌地图 ,gmaps4rails和,并且无法清除如何在单击 marker 上的自动平移映射时删除该事件。

标记是从我的控制器发送的:

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,为unclickable设置标记,然后在单击时添加一个侦听器。 所以我想我做了错误,但找不到什么 ! 任何帮助都会非常感激。 许多 Tahnks

Edit: 根据 @apneadiving, 建议,为了删除 @markers.panTo 行,在自定义生成器中重写infowindow_binding方法,但在单击它时仍然自动在 marker 上自动居中。。 下面是自定义生成器的代码:

`

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

如果任何帮助都是伟大的。 十分 感谢

时间:原作者:6个回答

0 0

我也遵循apneadiving的建议,它对我也有效。 如你所见,我将下面的inside assets/javascripts/gmaps4rails-infoxbox.coffee. 放到了 gem 中,然后注释了关于平移的行。

class @InfoBoxBuilder extends Gmaps.Google.Builders.Marker # inherit from base builder
 @CURRENT_INFOWINDOW: undefined
 @CACHE_STORE: {}
 # args:
 # lat
 # lng
 # infowindow
 # marker_title
 # picture
 # anchor: [x,y]
 # url
 # width
 # height
 # shadow
 # anchor: [x,y]
 # url
 # width
 # height
 # provider options:
 # https://developers.google.com/maps/documentation/javascript/reference?hl=fr#MarkerOptions
 # internal_options
 # singleInfowindow: true/false
 # maxRandomDistance: null/int in meters
 constructor: (@args, @provider_options = {}, @internal_options = {})->
 @before_init()
 @create_marker()
 @create_infowindow_on_click()
 @after_init()
 build: ->
 @marker = new(@model_class())(@serviceObject)
 create_marker: ->
 @serviceObject = new(@primitives().marker)(@marker_options())
 create_infowindow: ->
 return null unless _.isString @args.infowindow
 new(@primitives().infowindow)({content: @args.infowindow })
 marker_options: ->
 coords = @_randomized_coordinates()
 base_options =
 title: @args.marker_title
 position: new(@primitives().latLng)(coords[0], coords[1])
 icon: @_get_picture('picture')
 shadow: @_get_picture('shadow')
 _.extend @provider_options, base_options
 create_infowindow_on_click: ->
 @addListener 'click', @infowindow_binding
 infowindow_binding: =>
 @constructor.CURRENT_INFOWINDOW.close() if @_should_close_infowindow()
 # @marker.panTo()
 @infowindow?= @create_infowindow()
 return unless @infowindow?
 @infowindow.open( @getServiceObject().getMap(), @getServiceObject())
 @marker.infowindow?= @infowindow
 @constructor.CURRENT_INFOWINDOW = @infowindow
 _get_picture: (picture_name)->
 return null if!_.isObject(@args[picture_name]) ||!_.isString(@args[picture_name].url)
 @_create_or_retrieve_image @_picture_args(picture_name)
 _create_or_retrieve_image: (picture_args) ->
 if @constructor.CACHE_STORE[picture_args.url] is undefined
 @constructor.CACHE_STORE[picture_args.url] = new(@primitives().markerImage)(picture_args.url, picture_args.size, picture_args.origin, picture_args.anchor, picture_args.scaledSize)
 @constructor.CACHE_STORE[picture_args.url]
 _picture_args: (picture_name)->
 {
 url: @args[picture_name].url
 anchor: @_createImageAnchorPosition @args[picture_name].anchor
 size: new(@primitives().size)(@args[picture_name].width, @args[picture_name].height)
 scaledSize: null
 origin: null
 }
 _createImageAnchorPosition : (anchorLocation) ->
 return null unless _.isArray anchorLocation
 new(@primitives().point)(anchorLocation[0], anchorLocation[1])
 _should_close_infowindow: ->
 @internal_options.singleInfowindow and @constructor.CURRENT_INFOWINDOW?
 _randomized_coordinates: ->
 return [@args.lat, @args.lng] unless _.isNumber(@internal_options.maxRandomDistance)
 #gives a value between -1 and 1
 random = -> (Math.random() * 2 - 1)
 dx = @internal_options.maxRandomDistance * random()
 dy = @internal_options.maxRandomDistance * random()
 Lat = parseFloat(@args.lat) + (180/Math.PI)*(dy/6378137)
 Lng = parseFloat(@args.lng) + ( 90/Math.PI)*(dx/6378137)/Math.cos(@args.lat)
 return [Lat, Lng]
原作者:
...