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

[FEATURE] Covert LatLng to Point with map rotation in mind #1311

Closed
3 tasks done
mohammedX6 opened this issue Jul 18, 2022 · 23 comments · Fixed by #1330
Closed
3 tasks done

[FEATURE] Covert LatLng to Point with map rotation in mind #1311

mohammedX6 opened this issue Jul 18, 2022 · 23 comments · Fixed by #1330
Labels
feature This issue requests a new feature

Comments

@mohammedX6
Copy link

mohammedX6 commented Jul 18, 2022

What do you want implemented?

I want to covert a LatLng to point with taking care map rotation

minimal example of the problem I have https://filebin.net/t8uftfm4aklz69yx/minimal_example.zip

What other alternatives are available?

I tried this but the result is wrong

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

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

    var pos = _state.options.crs.latLngToPoint(localPoint, _state.zoom);

    final localPointCenterDistance =
        CustomPoint((width / 2) - pos.x, (height / 2) - pos.y);

    var mapCenter =
        _state.options.crs.latLngToPoint(_state.center, _state.zoom);
    var point = mapCenter - localPointCenterDistance;

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


    return point;
  }

Can you provide any other information?

No response

Platforms Affected

Android, iOS

Severity

Obtrusive: No workarounds are available, and this is essential to me

Requirements

@mohammedX6 mohammedX6 added the feature This issue requests a new feature label Jul 18, 2022
@ibrierley
Copy link
Collaborator

Whats your test case you are trying to do ?

@mohammedX6
Copy link
Author

Whats your test case you are trying to do ?

Hi
I am trying to create a magnifier for the map, The magnifier needs the tap position, everything is ok but if the map is rotated the tap position is wrong.

Take a look here

@JaffaKetchup
Copy link
Member

Hi @mohammedX6, I have a better understanding now.

The onTap callback within MapOptions gives a LatLng point, but also a TapPosition, which provides the actual screen position, both relative to the widget (relative), or relative to the screen (global). It should work regardless of map rotation.

For example, use:

MapOptions(
  onTap: (screenPoint, _) {
    print(screenPoint.relative);
    print(screenPoint.global);
  }
),

Let me know if this works for you!

@mohammedX6
Copy link
Author

mohammedX6 commented Jul 18, 2022

@JaffaKetchup Thanks for response, I am using flutter_map_line_editor onDragUpdate callback , using the screenPoint.global The result is much better now, But there is some offset as you can see in the picture
The blue circle is the current active drag position but the red circle is the wrong postion.
I think I need to subtract the position to the screen center?
Screenshot (6)

@JaffaKetchup
Copy link
Member

Maybe? There might be some weird anchoring going on, if you're using markers.

@mohammedX6
Copy link
Author

mohammedX6 commented Jul 18, 2022

no markers involved.

@JaffaKetchup
Copy link
Member

Where are the red circle and blue circles supposed to be?

@mohammedX6
Copy link
Author

the red circle must be in the same location as the blue circle.
The blue circle is the point that I am currently dragging.

@JaffaKetchup
Copy link
Member

Not really sure in this case. The position from onTap is coming from a dependency, and is correct from my testing, so there must be something else wrong.

@JaffaKetchup JaffaKetchup added the invalid This bug could not be reproduced or does not exist, or is very low quality label Jul 18, 2022
@mohammedX6
Copy link
Author

I think I might dig more into the code, Anyway thanks @JaffaKetchup .

@mohammedX6
Copy link
Author

@ibrierley Hi, here is an issue I opened,
Anyone can use this minmial example to understand the problem I have
https://filebin.net/t8uftfm4aklz69yx/minimal_example.zip

@ibrierley
Copy link
Collaborator

ok, that isn't a minimal example. But in the meantime, WHY do you want latLngToPoint. Just checking you're not trying to reinvent the wheel. (I'm not suggesting at some point there shouldn't be a method for this..but trying to understand why firstly).

For example, if you have a LatLng, why not just place a Marker there with any widget you want ? Does it need to be outside of FlutterMap or something ?

@mohammedX6
Copy link
Author

@ibrierley YES that's exactly what i want.

@ibrierley
Copy link
Collaborator

I suspect this isn't easily going to happen unless someone has a lot of time and/or been working on this already. I think there's probably too many variables that one can't be confident of and change when a rotation happens.

Maybe someone will have a brainwave though!

There's also the question of what space one wants the result in...in map widget space, in an outer widget space, or screen resolution space.

So my interim suggestion would be either a) don't allow rotation in the app, or b) make sure everything is done in flutter_map layer coordinate space (eg as a marker widget, I would suspect this is easiest), or c) spend a lot of time hacking something together.

@ibrierley
Copy link
Collaborator

Actually, this may be possible if we reverse the rotationPoint...and use a pre rotated pixel origin....something like...

@override
  CustomPoint latLngToScreenPoint(LatLng latLng) {

    var pxo = (_state.project(_state.getCenter(),zoom) - _state.originalSize! / 2.0).round();

    var point = _state.options.crs.latLngToPoint(latLng, _state.zoom);

    final mapCenter =
    _state.options.crs.latLngToPoint(_state.center, _state.zoom) ;

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

    return  point - pxo;
  }

CustomPoint<num> rotatePoint2(
      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);
  }


Maybe a little messy, but I've run out of time though for today.

@mohammedX6
Copy link
Author

mohammedX6 commented Jul 28, 2022

@ibrierley MAN, THIS THING IS WORKING LIKE A CHARM !,
You are genius
Where I can give a small gift like a donation or something?

@ibrierley
Copy link
Collaborator

Thanks for the feedback, I'll try and get a PR sorted this weekend for it (maybe with renaming the rotate functions so they're clearer), so it ends up in the main code. We don't have a Flutter_map donation thing yet (maybe that could be a discussion, not sure if we want to stay away from that or not). Normally devs will have a donation button on their respective repos though on Git if people feel flush tho ;).

@JaffaKetchup
Copy link
Member

JaffaKetchup commented Jul 29, 2022

We probably should setup an overall donation thing.
I've already got a GitHub Sponsors verified account now, and if you got one too (not difficult), GitHub allows us to add a button to this repo, and both of our names would be listed separately. This means people can donate to one or both of us freely.
I have this setup in my main repos if you want to have a look.

@mohammedX6
Copy link
Author

mohammedX6 commented Jul 29, 2022

Thanks for the feedback, I'll try and get a PR sorted this weekend for it (maybe with renaming the rotate functions so they're clearer), so it ends up in the main code. We don't have a Flutter_map donation thing yet (maybe that could be a discussion, not sure if we want to stay away from that or not). Normally devs will have a donation button on their respective repos though on Git if people feel flush tho ;).

@ibrierley
I don't see a donation button on your page, Can you send Ko-fi or PayPal donation link?

@ibrierley
Copy link
Collaborator

ibrierley commented Jul 29, 2022 via email

@ibrierley
Copy link
Collaborator

There's now a PR at #1325 if you wanted to test that.

@mohammedX6
Copy link
Author

I am confirmed it's working.

@JaffaKetchup JaffaKetchup removed the invalid This bug could not be reproduced or does not exist, or is very low quality label Aug 2, 2022
@JaffaKetchup
Copy link
Member

Fixed as of v2.2.0. Thanks for the report!

@JaffaKetchup JaffaKetchup linked a pull request Aug 2, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue requests a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants