Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Rounding Error when converting double values from TextField to Coordinates for MapItemView



  • I'm trying to add coordinates on a map in QML and then pass it to C++ to eventually send to a drone. I'm using a ListModel to store the coordinates but I'm having problems with the accuracy of the waypoint.

    ListModel {
        id:wpModel
    }
    
    Plugin {
        id: mapPlugin
        name: "mapboxgl"
     }
    

    The map is setup in QML like this

    Map{
            id: map
            anchors.fill:parent
            plugin: mapPlugin
            center: QtPositioning.coordinate(48.854314, 2.292297)
            zoomLevel:18
            copyrightsVisible: false
            tilt: 45
    
            MapItemView{
                model:wpModel
                delegate:MapQuickItem{
                    coordinate{
                        latitude: lat
                        longitude: lon
                    } 
                    sourceItem: Image{
                        id:waypointMarker
                        opacity: .75
                        sourceSize.width:80
                        sourceSize.height:80
                        source: "qrc:///marker-red.png"
                    }  
                anchorPoint.x: waypointMarker.width/2
                anchorPoint.y: waypointMarker.height
    
                }
            }
    
        }
    

    I then have two textfields with a DoubleValidator for the Latitude and Longitude of the waypoint.

    RowLayout{
         Label{
         text: "Start Lat:"
          } 
         TextField{
            id: startLatInput
            validator:DoubleValidator{
                 decimals:15
                 notation:DoubleValidator.ScientificNotation
            }
         }
         Label{
           text:"Start Lon:"
         }
         TextField{
            id: startLonInput
            validator:DoubleValidator{
            decimals:15
            notation:DoubleValidator.ScientificNotation
            }
          }  
      } //RowLayout
    

    And finally a button which appends the waypoint to the model

      Button{
         id: wpOkButton
         text: "OK"
         onClicked:{
            var startLat = parseFloat(startLatInput.text)
            var startLon = parseFloat(startLonInput.text)
            var val = map.toCoordinate(startLat, startLon)
            wpModel.append({lat: startLatInput.text, lon: startLonInput.text})                                                         
            }
       } // okButton
    

    The parsing of the startLon and startLat returns the same value as startLatInput.text and startLonInput.text but once I append to wpModel, the value changes and is completely different to my input. This is what an example output looks like:

    If we take, the following coordinate 52.756105, -1.247304 which is at the edge of the field on google maps and the output for MapQuickItem becomes

    qml: 52.75859356721886
    qml: -1.2512258791688566
    

    which is metres away from the intended waypoint. The same behaviour is found if I just hardcode the coordinates into a MapQuickItem i.e.

    MapQuickItem{
            sourceItem: Image{
                id:sample
                opacity: .75
                sourceSize.width:80
                sourceSize.height:80
                source: "qrc:///marker-red.png"
            }
            coordinate: QtPositioning.coordinate(52.756105, -1.247304)
            anchorPoint.x: sample.width/20
            anchorPoint.y: sample.height
    
        }
    

    However, if I physically click on the waypoint I want using a mouse, I get what I or close to it anyway,

    MouseArea{
      anchors.fill:parent
      focus:true
      hoverEnabled:false
      acceptedButtons: Qt.LeftButton
    
          onPressAndHold: {
                 var pos = map.toCoordinate(Qt.point(mouse.x,mouse.y));                 
                 console.log(pos.latitude)
                 console.log(pos.longitude)  
          }
    
      }
    

    This returns an output of

    qml: 52.75604121576361
    qml: -1.247375123866334
    

    which is kind of acceptable.

    Can someone please elaborate on what I'm doing wrong here?


Log in to reply