Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Предположительно неправильно работает screenToWorld #5

Open
iddqd-dev opened this issue May 25, 2024 · 0 comments

Comments

@iddqd-dev
Copy link

iddqd-dev commented May 25, 2024

Пытаюсь реализовать рисование области пальцем. Либо некорректно работает преобазование в методе screenToWorld, либо я что-то делаю не так.
zoom установленный по умолчанию.

import 'package:flutter/material.dart';
import 'package:yandex_maps_mapkit/mapkit.dart';
import 'package:yandex_maps_mapkit/yandex_map.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  MapWindow? _mapWindow;
  final List<Point> _polygonPoints = [];
  bool _isDrawing = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        onLongPressStart: _handleLongPressStart,
        onLongPressMoveUpdate: _handleLongPressMoveUpdate,
        onLongPressEnd: _handleLongPressEnd,
        child: YandexMap(
          onMapCreated: _onMapCreated,
        ),
      ),
    );
  }

  void _onMapCreated(MapWindow mapWindow) {
    _mapWindow = mapWindow;
  }

  void _handleLongPressStart(LongPressStartDetails details) {
    final RenderBox renderBox = context.findRenderObject() as RenderBox;
    print(renderBox.localToGlobal(details.globalPosition)); 
    final Point? point = _screenPointToMapPoint(renderBox.globalToLocal(details.globalPosition));
    setState(() {
      _polygonPoints.clear();
      _polygonPoints.add(point!);
      _isDrawing = true;
    });
  }

  void _handleLongPressMoveUpdate(LongPressMoveUpdateDetails details) {
    final RenderBox renderBox = context.findRenderObject() as RenderBox;
    final Point? point = _screenPointToMapPoint(renderBox.globalToLocal(details.localPosition));
    if (_isDrawing) {
      setState(() {
        _polygonPoints.add(point!);
      });
    }
  }

  Point? _screenPointToMapPoint(Offset localPosition) {
    final point = ScreenPoint(x: localPosition.dx, y: localPosition.dy);
    print(point);
    print(_mapWindow!.screenToWorld(point));
    return _mapWindow!.screenToWorld(point);
  }

  void _handleLongPressEnd(LongPressEndDetails details) {
    if (_isDrawing) {
      setState(() {
        _isDrawing = false;
        if (_polygonPoints.length > 3) {
          _drawPolygon();
        }
      });
    }
  }

  void _drawPolygon() {
    if (_mapWindow != null && _polygonPoints.isNotEmpty) {
      final polygon = Polygon(
        LinearRing(_polygonPoints), []
      );
      _mapWindow!.map.mapObjects.addPolygon(polygon);
    }
  }
}
I/flutter ( 7543): Offset(152.0, 362.1)
I/flutter ( 7543): ScreenPoint(x: 151.9950284090909, y: 362.14950284090907)
I/flutter ( 7543): Point(latitude: 78.99708352591344, longitude: 40.39708695628468)

в итоге область появляется ближе к верхнему левому углу при рисовании ближе к центру.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant