Skip to content

Commit

Permalink
Update lib/src/map/map.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup authored May 19, 2022
1 parent 047b963 commit d682baf
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,42 @@ class MapControllerImpl implements MapController {
return _state.rotate(degree, id: id, source: MapEventSource.mapController);
}

@override
LatLng? pointToLatLng(CustomPoint localPoint) {
if (_state.originalSize == null) {
return null;
}

final width = _state.originalSize!.x;
final height = _state.originalSize!.y;

final localPointCenterDistance =
CustomPoint((width / 2) - localPoint.x, (height / 2) - localPoint.y);
final mapCenter =
_state.options.crs.latLngToPoint(_state.center, _state.zoom);

var point = mapCenter - localPointCenterDistance;

if (_state.rotation != 0.0) {
point = rotatePoint(mapCenter, point);
}

return _state.options.crs.pointToLatLng(point, _state.zoom);
}

CustomPoint<num> rotatePoint(
CustomPoint<num> mapCenter, CustomPoint<num> point) {
final m = Matrix4.identity()
..translate(mapCenter.x.toDouble(), mapCenter.y.toDouble())
..rotateZ(-_state.rotationRad)
..translate(-mapCenter.x.toDouble(), -mapCenter.y.toDouble());

final tp = MatrixUtils.transformPoint(
m, Offset(point.x.toDouble(), point.y.toDouble()));

return CustomPoint(tp.dx, tp.dy);
}
@override
LatLng? pointToLatLng(CustomPoint localPoint) {
if (_state.originalSize == null) {
return null;
}

final width = _state.originalSize!.x;
final height = _state.originalSize!.y;

final localPointCenterDistance =
CustomPoint((width / 2) - localPoint.x, (height / 2) - localPoint.y);
final mapCenter =
_state.options.crs.latLngToPoint(_state.center, _state.zoom);

var point = mapCenter - localPointCenterDistance;

if (_state.rotation != 0.0) {
point = rotatePoint(mapCenter, point);
}

return _state.options.crs.pointToLatLng(point, _state.zoom);
}

CustomPoint<num> rotatePoint(
CustomPoint<num> mapCenter, CustomPoint<num> point) {
final m = Matrix4.identity()
..translate(mapCenter.x.toDouble(), mapCenter.y.toDouble())
..rotateZ(-_state.rotationRad)
..translate(-mapCenter.x.toDouble(), -mapCenter.y.toDouble());

final tp = MatrixUtils.transformPoint(
m, Offset(point.x.toDouble(), point.y.toDouble()));

return CustomPoint(tp.dx, tp.dy);
}

@override
Stream<MapEvent> get mapEventStream => _mapEventSink.stream;
}
Expand Down

0 comments on commit d682baf

Please sign in to comment.