flutter_map_extensions.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_map/flutter_map.dart';
  3. import 'package:latlong2/latlong.dart';
  4. import 'dart:math' as math;
  5. extension MoveByBounds on MapController {
  6. // TODO: Remove this in favor of built-in method when upgrading flutter_map to 5.0.0
  7. LatLng? centerBoundsWithPadding(
  8. LatLng coordinates,
  9. Offset offset, {
  10. double? zoomLevel,
  11. }) {
  12. const crs = Epsg3857();
  13. final oldCenterPt = crs.latLngToPoint(coordinates, zoomLevel ?? zoom);
  14. final mapCenterPoint = _rotatePoint(
  15. oldCenterPt,
  16. oldCenterPt - CustomPoint(offset.dx, offset.dy),
  17. );
  18. return crs.pointToLatLng(mapCenterPoint, zoomLevel ?? zoom);
  19. }
  20. CustomPoint<double> _rotatePoint(
  21. CustomPoint<double> mapCenter,
  22. CustomPoint<double> point, {
  23. bool counterRotation = true,
  24. }) {
  25. final counterRotationFactor = counterRotation ? -1 : 1;
  26. final m = Matrix4.identity()
  27. ..translate(mapCenter.x, mapCenter.y)
  28. ..rotateZ(degToRadian(rotation) * counterRotationFactor)
  29. ..translate(-mapCenter.x, -mapCenter.y);
  30. final tp = MatrixUtils.transformPoint(m, Offset(point.x, point.y));
  31. return CustomPoint(tp.dx, tp.dy);
  32. }
  33. double getTapThresholdForZoomLevel() {
  34. const scale = [
  35. 25000000,
  36. 15000000,
  37. 8000000,
  38. 4000000,
  39. 2000000,
  40. 1000000,
  41. 500000,
  42. 250000,
  43. 100000,
  44. 50000,
  45. 25000,
  46. 15000,
  47. 8000,
  48. 4000,
  49. 2000,
  50. 1000,
  51. 500,
  52. 250,
  53. 100,
  54. 50,
  55. 25,
  56. 10,
  57. 5,
  58. ];
  59. return scale[math.max(0, math.min(20, zoom.round() + 2))].toDouble() / 6;
  60. }
  61. }