1

Am working in an angular.dart project that uses gmaps. And i want to get the LatLng value on map click. My map.html :

<div id="map" ng-click="ctrl.mapClick($event)"></div>

My DartCtrl :

 void mapClick(e){
    print(e.runtimeType); //MouseEvent
    print(e.latLng); //error : Undifined function mapClick ...
 }

Any idea to get LatLng value without writing something like :

map.onClick.listen((e){
print(e.latLng);
});

*This happend in dartium so its not related to dart2js. and think you ..

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
youssef
  • 493
  • 3
  • 11
  • Does this work? `map.onClick.listen((e){ print(e.latLng); });` – Günter Zöchbauer May 16 '14 at 16:10
  • yeah sure where map is querySelector('#map') – youssef May 16 '14 at 16:16
  • I can' believe that. The `MouseEvent` has no `latLng` getter, how should that work? Can you check if the `e.detail` provides any useful information? – Günter Zöchbauer May 16 '14 at 16:19
  • `e.detail` prints `1`, its supposed work because it uses Dart style listener – youssef May 16 '14 at 16:24
  • What is supposed to work? Your second example? As far as I know you get the same 'dart:html' `MouseEvent` object and it does *not* have a `latLng` getter in both cases. – Günter Zöchbauer May 16 '14 at 16:27
  • Well thats always the case with gmaps, i gess that it alter the MouseEvent object because when i ask for the runtimeType in the listner i get something like `MouseEvent (:1)` – youssef May 16 '14 at 16:36
  • Runtime type isn't very reliable in Dart (just for debugging purposes). But I suppose as well that GMap adds a property to the event object. You need dart-js-interop to access this property because Dart doesn't support dynamically added fields/methods on classes. See my answer for more details. – Günter Zöchbauer May 16 '14 at 17:02
  • Are you using [google_maps package](http://pub.dartlang.org/packages/google_maps) to instantiate the map or are you creating the map directly with `dart:js` ? – Alexandre Ardhuin May 16 '14 at 20:10
  • yeah am using [google_maps package](http://pub.dartlang.org/packages/google_maps) to handle the map. after some digging i found that when using listener [google_maps package](http://pub.dartlang.org/packages/google_maps) creates a MouseEvent from the package MouseEvent event that have the `latLng` property. that explain the difference between $event's MouseEvent and listeners 'MouseEvent'. – youssef May 17 '14 at 21:10

2 Answers2

0

I think you need something like

import 'dart:js';

...

new JsObject.fromBrowserObject(e)['latLng']

(not tested)

for Dart-js-interop see also js interop compiled with dart2js error - Uncaught NoSuchMethodError : method not found:

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

The MouseEvent returned is not the native one neither inherit from HTML's MouseEvent, it's 'src/generated/core/events/mouse_event.dart' so there s no possible way that my example work. A better approach would be to create an NgDecorator that have a callback method that returns the special MouseEvent wish contains the LatLng

youssef
  • 493
  • 3
  • 11