[Interest] QtLocation MapPolyLine and MouseArea

Jérôme Godbout godboutj at amotus.ca
Thu Mar 7 21:46:48 CET 2019

If you ever need that approach you could avoid the qsqrt() and compare the square distance instead, that would save some time (but yes it still heavy). If you have a line thickness threshold you could square it when you receive it only once.
The best would be to have a shader doing it for you, you could render the map into a special shader and check the trace color/distance into the pixel and the mouse position. Not sure you can have a property output from that or if Qml ShaderEffect can be used that way. 
Maybe a pixel image from the map could be useful and simply do a pixel color compare? This is a really cheap way to do this but could worth a try. Every Item can be render to an image with grabToImage(), this is probably costly too.
If you find a solution I would be interested into the final solution of this.

-----Original Message-----
From: Interest <interest-bounces at qt-project.org> On Behalf Of maitai
Sent: March 7, 2019 3:35 PM
To: Interest at qt-project.org
Subject: Re: [Interest] QtLocation MapPolyLine and MouseArea

Yes thanks I know the maths for that, the problem being it's going to be calculated for all lines every time the user touches the mouse.

Just to share here is my routine to calculate a QPointF distance to a

#define DIST(P1, P2) ((P1.x() - P2.x()) * (P1.x() - P2.x()) + (P1.y() -
P2.y()) * (P1.y() - P2.y()))

double Util::distToSegment(const QPointF &point,const QLineF &line) {
     const double d1 = DIST(line.p1(),line.p2());
     if(d1 == 0.0)
         return qSqrt(DIST(point, line.p1()));
     const double t = ((point.x() - line.p1().x()) * (line.p2().x() -
               + (point.y() - line.p1().y()) * (line.p2().y() -
line.p1().y())) / d1;
     if(t < 0)
         return qSqrt(DIST(point, line.p1()));
         return qSqrt(DIST(point, line.p2()));
     return qSqrt(DIST(point, QPointF(line.p1().x() + t * (line.p2().x()
- line.p1().x()),
                                    line.p1().y() + t * (line.p2().y() - line.p1().y())))); }

I doubt there is a faster solution, still it's heavy if you need to do that on each mouse move, for all lines/segments you need to iterate to find the smallest.

Philippe Lelong

Le 07-03-2019 20:28, Jérôme Godbout a écrit :
> Just throwing an idea, maybe you could convert the mouse click or 
> hover to coordinate on the Map::toCoordinate() and interpolate the 
> coordinate to see with parameter to the line vector and then find the 
> distance to that line parameter point or min/max point. This is not 
> super fast but could be quick enough. Maybe doing the inverted 
> converting the coordinate to point would make the computation easier.
> http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
> -----Original Message-----
> From: Interest <interest-bounces at qt-project.org> On Behalf Of maitai
> Sent: March 7, 2019 12:33 PM
> To: Interest at qt-project.org
> Subject: [Interest] QtLocation MapPolyLine and MouseArea
> Hi,
> I need to trigger various actions whenever a MapPolyLine is hovered or 
> pressed, such as displaying a tooltip, a menu, etc.
> I have put a MouseArea on it with anchors.fills: parent, but the 
> problem is that the mouse area does not represent the line, but the 
> polygon made by the line. For instance if you have a L shape, entered 
> event and so on is triggered when you enter the bounding rectangle of 
> the line, not when you hover over the line itself.
> On a QGraphicsScene we had the shape() protected method for that kinds 
> of case, for instance with a QPainterPathStroker to give some 
> thickness to the line's "mousearea".
> I will probably end with a custom property that will carry the pixel 
> distance between the line segments and the mouse coordinates, but this 
> is going to be heavy to compute (I have potentially hundreds of 
> complicated lines on the map).
> Is there a better way or even better a standard way to do that?
> Thanks
> Philippe Lelong
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
Interest mailing list
Interest at qt-project.org

More information about the Interest mailing list