Call google maps api event addListener using dart:js

Issue

In the Google Maps API v3 they have stated that we need to do this to open the infowindow when the marker gets clicked:

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
});

Now I am trying to duplicate this in dart using the js library. So far I have something like this:

final google_maps = context['google']['maps'];

var myLatlng = [43.5, -6.5];
var center = new JsObject(google_maps['LatLng'], myLatlng);

var mapTypeId = google_maps['MapTypeId']['ROADMAP'];

var mapOptions = new JsObject.jsify({
  "center": center,
  "zoom": 8,
  "mapTypeId": mapTypeId
});

var map = new JsObject(google_maps['Map'], [querySelector('#map-canvas'), mapOptions]);

var marker = new JsObject(google_maps['Marker'], [new JsObject.jsify({
  'position': center,
  'map': map,
  'title': 'Hello World!'
})]);

var tooltip = '<div id="content">Info window coontent</div>';

var infowindow = new JsObject(google_maps['InfoWindow'], [new JsObject.jsify({
"content": tooltip
})]);


google_maps['event'].callMethod('addListener', [marker, 'click', () {
  infowindow.callMethod('open',[map,marker]);
}]);

The issue is that I set the ‘addListener’ method through google_maps[‘event’], but when I click the marker, I get a NoSuchMethodError:

Closure call with mismatched arguments: function 'call'

NoSuchMethodError: incorrect number of arguments passed to method named 'call'
Receiver: Closure: () => dynamic
Tried calling: call(Instance of 'JsObject')
Found: call()

I am aware that there is a google_maps dart package, but I want the interact with the javascript api using dart’s js library.

Thanks in advance.

Solution

Your closure has zero arguments.

() {
   infowindow.callMethod('open',[map,marker]);
}

You just have to give it an argument as stated in the error message:

(event) {
   infowindow.callMethod('open',[map,marker]);
}

Answered By – Robert

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *