Unexpected behavior with TapHandler in MapPolyline
-
Using Qt 6.5.3 on Windows 11.
I have a MapPolyline displayed on a map and want to select a specific point on that line with a mouse click.
Is there an available standard way to do this?
All I could figure out was for the MapPolyline to handle mouse events itself and to search the points it its path for the point "closest" to the point clicked. This almost works.
The problem is that the TapHandler in MapPolyline reports the clicked coordinate incorrectly. Specifically, the TapHandler for the overall map seems to report the correct coordinate for points near the MapPolyline, but the TapHandler for the polyline reports a coordinate that is substantially offset (well outside the bounds of the full polyline) from the nearby points.
Does this behavior make sense to anyone? Any suggestions where to look for resolution?
Thanks
-
Let me try this again. On the off chance that something confusing in my initial description led to the lack of any response, I will make this a bit more explicit. Following my description of this example, I provide the only potentially meaningful hit I have thought of, in the hopes that it means enough to someone that they might point me in the next useful direction toward solution.
Following is a stub of my code, which began as the standard mapviewer example from QtLocation. MapView uses the OSM plugin to display map tiles. It also contains various additional items that can be displayed on this map. Of current interest are routes and tracks as would be displayed by any GPS device. The mapviewer example does that using a MapPolyline. The code below shows the TapHandlers for both the MapView and for the MapPolyline. That for the MapView logs the coordinates of map clicks to the main output widget and then adds a waypoint at the clicked point. That for the MapPolyline logs what it thinks is the clicked coordinate and then adds a waypoint at that coordinate.
MapView { id: standIn // bunch more stuff TapHandler { id: mMcTapHandler property variant lastCoordinate onTapped: { lastCoordinate = standIn.map.toCoordinate(mMcTapHandler.point.position) var msg = "MapComponent:mMcTapHandler - onTapped: - " + Helper.formatCoordinate(lastCoordinate) mainOut.log(msg) mMC.addMarkerFromMap(lastCoordinate) } MapPolyline { id: myMapPolylineItem // some more stuff TapHandler { id: polyTapHandler property variant lastCoordinate onTapped: { lastCoordinate = standIn.map.toCoordinate(polyTapHandler.point.position) var msg = "MyPolylineItem:polyTapHandler - onTapped: - " + Helper.formatCoordinate(lastCoordinate) mainOut.log(msg) mMC.addMarkerFromPolyline(lastCoordinate) } } }
Below is a screen shot that demonstrates my problem. I have defined a "route" by three waypoints - the red markers numbered 1 to 3, which are connect6ed by the MapPolyline. Also marked are 6 additional waypoints I got by clicking pairs points on near the polyline (one pair on the upright leg and one pair on the horizontal leg) and by clicking on the polyline between each pair of points. These are the blue waypoints numbered 1 to 6.
The following is from the main log output and shows what the MapPolyline has for its three coordinates, along with the coordinates found by clicking both the MapView and the MapPolyline.
Polyline coordinates (1) - 38.1800000, -107.6832810 (2) - 38.1950500, -107.6832810 (3) - 38.1950500, -107.6657370 1: MapComponent:mMcTapHandler - onTapped: (38.1873230, -107.6851949) 2: MapComponent:mMcTapHandler - onTapped: (38.1872555, -107.6818475) 3: MyPolylineItem:polyTapHandler - onTapped: (38.1946393, -107.6994685) 4: MapComponent:mMcTapHandler - onTapped: (38.1929222, -107.6743803) 5: MapComponent:mMcTapHandler - onTapped: (38.1966997, -107.6744661) 6: MyPolylineItem:polyTapHandler - onTapped: (38.2023963, -107.6903704)
I hope this clearly illustrates what is going on.
My only meaningful clue is that the offset between the "actual" coordinates and the "reported" coordinates is map-scale and map-location dependent. The reported coordinates are offset by approximately (but perhaps not exactly) by the offset of the MapPolyline's waypoint #2 from the northwest (upper left) corner of the map.
The MapPolyline is a Component that has been added to an Item that has been added to the MapView. Is there perhaps some coordinate translation, which could take place in the TapHandler, that shifts coordinates between the main MapView and Items or Components that are displayed on it. I am not aware that this is the case and wouldn't know how to deal with it if that is what is happening.
Many thanks for your time reading this lengthy post.