0
votes

I want to toggle open and close Infowindow divs on markers on my google map in a Backbone.ja app.

In my view I have an initialize function that has google.maps.event.addListener.

function initialize(viewOptions, app) {
  this.app = app;
  this.address = '';
  this.terms = [
      'Food',
      'Bar'
    ];
  // this.listenTo(this.collection, 'reset', this.render);
  this.listenTo(this.collection, 'add', this.addmarker);
  this.render();
  google.maps.event.addListener(map, 'click', function() {
    console.log('Clicked')
    // infoclicker.call(); 
  });
};

This addListener does not seem to work. When I click a marker, or even just anywhere on the map it does not trigger console.log. What am I doing wrong here?

Also FYI: there is a _setMap function called by the render function, that sets the map.

function _setMap(zoom, lat, long) {
  var mapOptions = {
      zoom: zoom ? zoom : 15,
      center: new google.maps.LatLng(lat ? lat : 45.5200,long ? long : -122.6819)
    };

  this.map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
};
2
What is map in that addListener call? - mu is too short
I believe 'map' is instance of the google map object created in the _setMap function. - nilesvm
Is there a #map-canvas when you call _setMap? - mu is too short
There is a #map-canvas in the map template when _setmap is called. - nilesvm
Just in the template or on the page itself? Have you checked that document.getElementById('map-canvas') is returning what you expect it to? - mu is too short

2 Answers

1
votes

This should do it:

google.maps.event.addListener(this.map, 'click', function() {
  console.log('Clicked');
});
0
votes

Check the scope of the variables you are using for the map and marker. They should be in scope of the function in which you are trying to add the action listener on them.