Skip to content

Commit

Permalink
Fixes for polar projection
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefWN committed Jun 30, 2022
1 parent bee620c commit 9f1f77a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
14 changes: 5 additions & 9 deletions lib/src/layer/circle_layer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/map/map.dart';
Expand Down Expand Up @@ -66,18 +68,12 @@ class CircleLayer extends StatelessWidget {
builder: (BuildContext context, _) {
final circleWidgets = <Widget>[];
for (final circle in circleOpts.circles) {
var pos = map.project(circle.point);
pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) -
map.getPixelOrigin();
circle.offset = Offset(pos.x.toDouble(), pos.y.toDouble());
circle.offset = map.getOffset(circle.point);

if (circle.useRadiusInMeter) {
final r = const Distance().offset(circle.point, circle.radius, 180);
var rpos = map.project(r);
rpos = rpos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) -
map.getPixelOrigin();

circle.realRadius = rpos.y - pos.y;
final delta = circle.offset - map.getOffset(r);
circle.realRadius = delta.distance;
}

circleWidgets.add(
Expand Down
24 changes: 11 additions & 13 deletions lib/src/layer/overlay_image_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:flutter_map/src/core/bounds.dart';

class OverlayImageLayerOptions extends LayerOptions {
final List<OverlayImage> overlayImages;
Expand Down Expand Up @@ -67,20 +68,17 @@ class OverlayImageLayer extends StatelessWidget {
}

Positioned _positionedForOverlay(OverlayImage overlayImage) {
final zoomScale =
map.getZoomScale(map.zoom, map.zoom); // TODO replace with 1?
final pixelOrigin = map.getPixelOrigin();
final upperLeftPixel =
map.project(overlayImage.bounds.northWest).multiplyBy(zoomScale) -
pixelOrigin;
final bottomRightPixel =
map.project(overlayImage.bounds.southEast).multiplyBy(zoomScale) -
pixelOrigin;
// northWest is not necessarily upperLeft depending on projection
final bounds = Bounds<num>(
map.project(overlayImage.bounds.northWest) - map.getPixelOrigin(),
map.project(overlayImage.bounds.southEast) - map.getPixelOrigin(),
);

return Positioned(
left: upperLeftPixel.x.toDouble(),
top: upperLeftPixel.y.toDouble(),
width: (bottomRightPixel.x - upperLeftPixel.x).toDouble(),
height: (bottomRightPixel.y - upperLeftPixel.y).toDouble(),
left: bounds.topLeft.x.toDouble(),
top: bounds.topLeft.y.toDouble(),
width: bounds.size.x.toDouble(),
height: bounds.size.y.toDouble(),
child: Image(
image: overlayImage.imageProvider,
fit: BoxFit.fill,
Expand Down
8 changes: 3 additions & 5 deletions lib/src/layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,10 @@ class PolygonLayer extends StatelessWidget {
for (var i = 0; i < len; ++i) {
final point = points[i];

var pos = map.project(point);
pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) -
map.getPixelOrigin();
offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
final offset = map.getOffset(point);
offsets.add(offset);
if (i > 0) {
offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
offsets.add(offset);
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions lib/src/layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,10 @@ class PolylineLayer extends StatelessWidget {
for (var i = 0; i < len; ++i) {
final point = points[i];

var pos = map.project(point);
pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) -
map.getPixelOrigin();
offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
final offset = map.getOffset(point);
offsets.add(offset);
if (i > 0) {
offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
offsets.add(offset);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ class MapState {
return _pixelOrigin;
}

Offset getOffset(LatLng pos) {
final delta = project(pos) - getPixelOrigin();
return Offset(delta.x.toDouble(), delta.y.toDouble());
}

CustomPoint getNewPixelOrigin(LatLng center, [double? zoom]) {
final viewHalf = size / 2.0;
return (project(center, zoom) - viewHalf).round();
Expand Down

0 comments on commit 9f1f77a

Please sign in to comment.