flutter_map_dragmarker icon indicating copy to clipboard operation
flutter_map_dragmarker copied to clipboard

Scrolling map when dragging marker to left or top fails

Open alanjhill opened this issue 9 months ago • 4 comments

After upgrading Flutter Map -> 8.* and Flutter Map Dragmarker -> 8.*, the drag functionality when hitting the edge of the screen (left or top) is failing. The markers disappear and reappear when moving the map. Dragging to the right or bottom is fine.

I believe this is attributed to the change in the DragMarker class which was required for compatibility with flutter_map 8.*.

Version 8.0.1:

/// This method checks if the marker is in the current map bounds
bool inMapBounds({
  required final MapCamera mapCamera,
  required final Alignment markerWidgetAlignment,
}) {
  var pxPoint = mapCamera.projectAtZoom(point);

  final left =
      0.5 * size.width * ((alignment ?? markerWidgetAlignment).x + 1);
  final top =
      0.5 * size.height * ((alignment ?? markerWidgetAlignment).y + 1);
  final right = size.width - left;
  final bottom = size.height - top;

  final offset = Offset(pxPoint.dx - right, pxPoint.dy - bottom);

  return mapCamera.pixelBounds.contains(offset);
}

Same method in v7.0.0:

bool inMapBounds({
  required final MapCamera mapCamera,
  required final Alignment markerWidgetAlignment,
}) {
  var pxPoint = mapCamera.project(point);

  final left =
      0.5 * size.width * ((alignment ?? markerWidgetAlignment).x + 1);
  final top =
      0.5 * size.height * ((alignment ?? markerWidgetAlignment).y + 1);
  final right = size.width - left;
  final bottom = size.height - top;

  final bounds = Bounds(
    Point(pxPoint.x + left, pxPoint.y - bottom),
    Point(pxPoint.x - right, pxPoint.y + top),
  );

  return mapCamera.pixelBounds.containsPartialBounds(bounds);
}

alanjhill avatar Mar 10 '25 15:03 alanjhill

Good spot, oddly I only tested bottom & right...so would this fix it for you...(there may be a method already existing somewhere that could be reused which would be preferable, but I think containsPartialBounds was removed...

bool inMapBounds({
    required final MapCamera mapCamera,
    required final Alignment markerWidgetAlignment,
  }) {
    var pxPoint = mapCamera.projectAtZoom(point);

    final left =
        0.5 * size.width * ((alignment ?? markerWidgetAlignment).x + 1);
    final top =
        0.5 * size.height * ((alignment ?? markerWidgetAlignment).y + 1);
    final right = size.width - left;
    final bottom = size.height - top;

    final rect = Rect.fromPoints(Offset(pxPoint.dx - right, pxPoint.dy - bottom),
        Offset(pxPoint.dx + right, pxPoint.dy + bottom));

    return containsPartialBounds(mapCamera.pixelBounds,rect);
  }

  bool containsPartialBounds(pixelBounds,rect) {
    return (pixelBounds.left <= rect.right) &&
        (pixelBounds.right >= rect.left) &&
        (pixelBounds.top <= rect.bottom) &&
        (pixelBounds.bottom >= rect.top);
  }
}

ibrierley avatar Mar 10 '25 15:03 ibrierley

Thanks @ibrierley for the quick response.

I will test this out and get back to you.

alanjhill avatar Mar 10 '25 16:03 alanjhill

@ibrierley

Yep...that does the job.

Many thanks for the quick turnaround 👍

alanjhill avatar Mar 10 '25 16:03 alanjhill

Should be published as 8.0.2, thanks for the testing!

ibrierley avatar Mar 10 '25 16:03 ibrierley