使用dart:js调用google maps api event addListener

时间:2014-08-29 16:45:48

标签: google-maps google-maps-api-3 dart dart-js-interop

在Google Maps API v3中,他们声明我们需要在点击标记时打开infowindow:

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

现在我正在尝试使用js库在dart中复制它。到目前为止,我有这样的事情:

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]);
}]);

问题是我通过google_maps ['event']设置'addListener'方法,但是当我点击标记时,我得到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()

我知道有google_maps dart package,但我希望使用dart的js库与javascript api进行交互。

提前致谢。

1 个答案:

答案 0 :(得分:6)

你的闭包没有参数。

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

你只需要给出一个参数,如错误信息中所述:

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