点击它时无法阻止自动平移到标记(谷歌地图)

时间:2014-12-13 12:15:03

标签: javascript google-maps google-maps-api-3 gmaps4rails

我使用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

` 任何帮助都会很棒....非常感谢

1 个答案:

答案 0 :(得分:0)

我也遵循了Apneadiving的建议,这对我来说很好。我把以下内容放在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]