0

I want to add event listener to all markers, i'm using Google-Maps-for-Rails gem. This is my code

  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()

I tried to add event listener to all marker but it doesnt alert anything. How can i add event listeners in google maps for rails

I have an array of data, where each data contain id, lat, lng

1
  • You can try to check this documentation about Google Maps with Rails. It covers in this documentation on how to add event listener in the marker. So just check the example code on how to properly implement it.
    – KENdi
    Mar 9, 2017 at 13:41

1 Answer 1

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()

the above code marker.serviceObject actually able to get the click event on the marker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.