Google地图会将侦听器添加到标记中

时间:2017-03-07 11:36:08

标签: ruby-on-rails ruby-on-rails-3 google-maps google-maps-api-3

我想为所有标记添加事件监听器,我正在使用Google-Maps-for-Rails gem。这是我的代码

  markers_from_api = data.bookings
  handler = Gmaps.build('Google')
  settings = {
    internal: {
      id: 'multi_markers'
    },
    provider:{
      styles: mapStyles
    }
  }
  handler.buildMap settings, ->
    markers = handler.addMarkers(markers_from_api)
    _.each markers,(i, j) ->
      google.maps.event.addListener i, 'click', (object) ->
        alert 'hello'
        return
    createSidebar markers_from_api
    createSidebarLi markers_from_api
    handler.bounds.extendWith markers
    handler.fitMapToBounds()

我尝试将事件监听器添加到所有标记但它不会发出任何警报。如何在google地图中为rails添加事件监听器

我有一个数据数组,其中每个数据都包含id,lat,lng

1 个答案:

答案 0 :(得分:0)

  markers_from_api = data.bookings
  handler = Gmaps.build('Google')
  settings = {
    internal: {
      id: 'multi_markers'
    },
    provider:{
      styles: mapStyles
    }
  }
  handler.buildMap settings, ->
    markers = handler.addMarkers(markers_from_api)
    _.each markers,(marker, j) ->
      google.maps.event.addListener marker.serviceObject, 'click', (object) ->
        console.log marker.serviceObject.position.lng()
        render_marker_data markers_from_api[j].id
        return
    handler.bounds.extendWith markers
    handler.fitMapToBounds()

上面的代码marker.serviceObject实际上能够在标记上获得click事件。