a14n / dart-google-maps

A library to use Google Maps JavaScript API v3 from Dart scripts.
Apache License 2.0
130 stars 66 forks source link

map.projection.fromLatLngToPoint not correctly bound #103

Closed ditman closed 3 years ago

ditman commented 3 years ago

I'm trying to write something like this:

// original JS by: Krasimir (https://krasimirtsonev.com/blog/article/google-maps-api-v3-convert-latlng-object-to-actual-pixels-point-object)
// (This is the reverse of [_pixelToLatLng])
gmaps.Point _latLngToPixel(gmaps.GMap map, gmaps.LatLng coords) {
  final zoom = map.zoom;
  final bounds = map.bounds;
  final projection = map.projection;

  assert(
      bounds != null, 'Map Bounds required to compute screen x/y of LatLng.');
  assert(projection != null,
      'Map Projection required to compute screen x/y of LatLng.');
  assert(zoom != null,
      'Current map zoom level required to compute screen x/y of LatLng.');

  final ne = bounds!.northEast;
  final sw = bounds.southWest;

  final topRight = projection!.fromLatLngToPoint!(ne)!;

  final bottomLeft = projection.fromLatLngToPoint!(sw)!;

  final scale = 1 << (zoom!.toInt()); // 2 ^ zoom

  final worldPoint = projection.fromLatLngToPoint!(coords)!;

  return gmaps.Point(
    ((worldPoint.x! - bottomLeft.x!) * scale).toInt(),
    ((worldPoint.y! - topRight.y!) * scale).toInt(),
  );
}

However, the above fails like so:

Uncaught (in promise) TypeError: Cannot read property 'x' of undefined
    at _.Jh.fromLatLngToPoint (js?key=AIzaSyAa9cRBkhuxGq3Xw3HPz8SPwaVOhRmm7kk:formatted:7183)
    at Object._latLngToPixel (convert.dart:463)
    at google_maps_flutter_web.GoogleMapController.new.getScreenCoordinate (google_maps_controller.dart:296)
    at getScreenCoordinate.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)
    at google_maps_flutter_web.GoogleMapController.new.getScreenCoordinate (google_maps_controller.dart:292)
    at google_maps_flutter_web.GoogleMapsPlugin.new.getScreenCoordinate (google_maps_flutter_web.dart:153)
    at google_maps_flutter.GoogleMapController.__.getScreenCoordinate (controller.dart:225)
    at main.MyApp.new.<anonymous> (main.dart:17)
    at Generator.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)
    at main.dart:15
    at google_maps_flutter._GoogleMapState.new.onTap (google_map.dart:435)

It seems that at some point (in the backing JS), calling fromLatLngToPoint has this incorrectly bound to Window:

Screen Shot 2021-08-09 at 5 04 15 PM

(Is this a regression/very similar issue to #87? Is this me holding this wrong? I'm working on this issue.)

a14n commented 3 years ago

Thanks for the report. I manage to reproduce and fix the issue. A new version is coming.

a14n commented 3 years ago

Fixed via b06cfbd2dde2c247b3fd4638ccc664683c0844c4 and available in 5.2.0 .

ditman commented 3 years ago

Thanks for the super fast fix @a14n, you're a super star!