From 36f3658c531dfc4b8b2aa7cccc751c271eccfe1e Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 2 Nov 2023 14:06:08 +0100 Subject: [PATCH] Remove dangerous Point extensions which were used to cover up deeper issues. --- lib/src/layer/marker_layer.dart | 3 +- lib/src/layer/overlay_image_layer.dart | 14 +++--- .../layer/polygon_layer/polygon_layer.dart | 1 - lib/src/layer/tile_layer/tile_layer.dart | 5 +-- .../tile_layer/wms_tile_layer_options.dart | 5 +-- lib/src/map/camera/camera.dart | 5 +-- lib/src/misc/point_extensions.dart | 44 +------------------ 7 files changed, 14 insertions(+), 63 deletions(-) diff --git a/lib/src/layer/marker_layer.dart b/lib/src/layer/marker_layer.dart index 490ad7d73..6b8e231a3 100644 --- a/lib/src/layer/marker_layer.dart +++ b/lib/src/layer/marker_layer.dart @@ -4,7 +4,6 @@ import 'package:flutter/widgets.dart'; import 'package:flutter_map/src/layer/general/mobile_layer_transformer.dart'; import 'package:flutter_map/src/map/camera/camera.dart'; import 'package:flutter_map/src/misc/bounds.dart'; -import 'package:flutter_map/src/misc/point_extensions.dart'; import 'package:latlong2/latlong.dart'; /// A container for a [child] widget located at a geographic coordinate [point] @@ -126,7 +125,7 @@ class MarkerLayer extends StatelessWidget { )) continue; // Apply map camera to marker position - final pos = pxPoint.subtract(map.pixelOrigin); + final pos = pxPoint - map.pixelOrigin; yield Positioned( key: m.key, diff --git a/lib/src/layer/overlay_image_layer.dart b/lib/src/layer/overlay_image_layer.dart index 531b51061..a83a9f906 100644 --- a/lib/src/layer/overlay_image_layer.dart +++ b/lib/src/layer/overlay_image_layer.dart @@ -66,8 +66,8 @@ class OverlayImage extends BaseOverlayImage { }) { // northWest is not necessarily upperLeft depending on projection final bounds = Bounds( - camera.project(this.bounds.northWest).subtract(camera.pixelOrigin), - camera.project(this.bounds.southEast).subtract(camera.pixelOrigin), + camera.project(this.bounds.northWest) - camera.pixelOrigin, + camera.project(this.bounds.southEast) - camera.pixelOrigin, ); return Positioned( @@ -111,12 +111,10 @@ class RotatedOverlayImage extends BaseOverlayImage { required Image child, required MapCamera camera, }) { - final pxTopLeft = - camera.project(topLeftCorner).subtract(camera.pixelOrigin); + final pxTopLeft = camera.project(topLeftCorner) - camera.pixelOrigin; final pxBottomRight = - camera.project(bottomRightCorner).subtract(camera.pixelOrigin); - final pxBottomLeft = - camera.project(bottomLeftCorner).subtract(camera.pixelOrigin); + camera.project(bottomRightCorner) - camera.pixelOrigin; + final pxBottomLeft = camera.project(bottomLeftCorner) - camera.pixelOrigin; /// calculate pixel coordinate of top-right corner by calculating the /// vector from bottom-left to top-left and adding it to bottom-right @@ -129,7 +127,7 @@ class RotatedOverlayImage extends BaseOverlayImage { final vectorX = (pxTopRight - pxTopLeft) / bounds.size.x; final vectorY = (pxBottomLeft - pxTopLeft) / bounds.size.y; - final offset = pxTopLeft.subtract(bounds.topLeft); + final offset = pxTopLeft - bounds.topLeft; final a = vectorX.x; final b = vectorX.y; diff --git a/lib/src/layer/polygon_layer/polygon_layer.dart b/lib/src/layer/polygon_layer/polygon_layer.dart index fb3f083e8..10f0b301b 100644 --- a/lib/src/layer/polygon_layer/polygon_layer.dart +++ b/lib/src/layer/polygon_layer/polygon_layer.dart @@ -5,7 +5,6 @@ import 'package:flutter_map/src/geo/latlng_bounds.dart'; import 'package:flutter_map/src/layer/general/mobile_layer_transformer.dart'; import 'package:flutter_map/src/layer/polygon_layer/label.dart'; import 'package:flutter_map/src/map/camera/camera.dart'; -import 'package:flutter_map/src/misc/bounds.dart'; import 'package:flutter_map/src/misc/point_extensions.dart'; import 'package:latlong2/latlong.dart' hide Path; // conflict with Path from UI diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index 3fbe08627..db4d6cf2b 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -25,7 +25,6 @@ import 'package:flutter_map/src/layer/tile_layer/tile_update_transformer.dart'; import 'package:flutter_map/src/map/camera/camera.dart'; import 'package:flutter_map/src/map/controller/map_controller.dart'; import 'package:flutter_map/src/misc/bounds.dart'; -import 'package:flutter_map/src/misc/point_extensions.dart'; import 'package:http/retry.dart'; import 'package:logger/logger.dart'; @@ -533,8 +532,8 @@ class _TileLayerState extends State with TickerProviderStateMixin { ); final currentPixelOrigin = Point( - map.pixelOrigin.x.toDouble(), - map.pixelOrigin.y.toDouble(), + map.pixelOrigin.x, + map.pixelOrigin.y, ); _tileScaleCalculator.clearCacheUnlessZoomMatches(map.zoom); diff --git a/lib/src/layer/tile_layer/wms_tile_layer_options.dart b/lib/src/layer/tile_layer/wms_tile_layer_options.dart index 2fb33ddf7..82bdd1091 100644 --- a/lib/src/layer/tile_layer/wms_tile_layer_options.dart +++ b/lib/src/layer/tile_layer/wms_tile_layer_options.dart @@ -69,9 +69,8 @@ class WMSTileLayerOptions { } String getUrl(TileCoordinates coords, int tileSize, bool retinaMode) { - final tileSizePoint = Point(tileSize, tileSize); - final nwPoint = coords.scaleBy(tileSizePoint); - final sePoint = nwPoint + tileSizePoint; + final nwPoint = coords * tileSize; + final sePoint = nwPoint + Point(tileSize, tileSize); final nwCoords = crs.pointToLatLng(nwPoint, coords.z.toDouble()); final seCoords = crs.pointToLatLng(sePoint, coords.z.toDouble()); final nw = crs.projection.project(nwCoords); diff --git a/lib/src/map/camera/camera.dart b/lib/src/map/camera/camera.dart index 6cdc2cbcb..c02392a69 100644 --- a/lib/src/map/camera/camera.dart +++ b/lib/src/map/camera/camera.dart @@ -281,8 +281,7 @@ class MapCamera { /// This will convert a latLng to a position that we could use with a widget /// outside of FlutterMap layer space. Eg using a Positioned Widget. Point latLngToScreenPoint(LatLng latLng) { - final nonRotatedPixelOrigin = - (project(center, zoom) - nonRotatedSize / 2.0).round(); + final nonRotatedPixelOrigin = project(center, zoom) - nonRotatedSize / 2.0; var point = crs.latLngToPoint(latLng, zoom); @@ -292,7 +291,7 @@ class MapCamera { point = rotatePoint(mapCenter, point, counterRotation: false); } - return point.subtract(nonRotatedPixelOrigin); + return point - nonRotatedPixelOrigin; } LatLng pointToLatLng(Point localPoint) { diff --git a/lib/src/misc/point_extensions.dart b/lib/src/misc/point_extensions.dart index dd1568100..ac4ab23f3 100644 --- a/lib/src/misc/point_extensions.dart +++ b/lib/src/misc/point_extensions.dart @@ -67,54 +67,12 @@ extension PointExtension on Point { /// Point(1, 2).subtract(1.5) would cause a runtime error because the /// resulting x/y values are doubles and the return value is a Point since /// the method returns Point. -/// -/// Note that division methods (unscaleBy and the / operator) are defined on -/// Point with a Point return argument because division -/// always returns a double. extension DoublePointExtension on Point { /// Subtract [other] from this Point. + @Deprecated('camera.pixelOrigin is now a Point. Prefer operator-.') Point subtract(Point other) { return Point(x - other.x, y - other.y); } - - /// Add [other] to this Point. - Point add(Point other) { - return Point(x + other.x, y + other.y); - } - - /// Create a new [Point] where [x] and [y] values are scaled by the respective - /// values in [other]. - Point scaleBy(Point other) { - return Point(x * other.x, y * other.y); - } -} - -/// This extension contains methods which, if defined on Point, -/// could cause a runtime error when called on a Point with a non-int -/// argument. An example: -/// -/// Point(1, 2).subtract(1.5) would cause a runtime error because the -/// resulting x/y values are doubles and the return value is a Point since -/// the method returns Point. -/// -/// The methods in this extension only take Point arguments to prevent -/// this. -extension IntegerPointExtension on Point { - /// Subtract [other] from this Point. - Point subtract(Point other) { - return Point(x - other.x, y - other.y); - } - - /// Add [other] to this Point. - Point add(Point other) { - return Point(x + other.x, y + other.y); - } - - /// Create a new [Point] where [x] and [y] values are scaled by the respective - /// values in [other]. - Point scaleBy(Point other) { - return Point(x * other.x, y * other.y); - } } extension OffsetToPointExtension on Offset {