Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dynamic Polyline in Qt Location/Map
QtWS25 Last Chance

Dynamic Polyline in Qt Location/Map

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 4 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mkhtrmn
    wrote on 4 Jul 2017, 15:05 last edited by mkhtrmn 7 Jun 2017, 08:21
    #1

    Hello,

    I want to create an application which allow me to draw polyline when I click two points somewhere on the map. Something like googlemap.

    I managed to draw one polyline, but how to create more than one polyline?
    main.qml

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import "componentCreation.js" as MyScript
    import QtQuick.Controls 2.1
    import QtLocation 5.3
    import QtPositioning 5.2
    
    Window {
        id: appWindow
        width: 512
        height: 512
        visible: true
        Map {
            id: map
            //width: win.width - kolom.width - row1.spacing
            anchors.fill: parent
            activeMapType: map.supportedMapTypes[2]
            zoomLevel: 1
            //z:1
    
    
            center {
                latitude: 5
                longitude: 100
            }
            plugin: Plugin {
                name: 'osm';
                PluginParameter {
                    name: 'osm.mapping.offline.directory';
                    value: ':/offline_tiles/'
                }
            }
            MapCircle {
                radius: 800000
                color: 'blue'
                center {
                    latitude: 5
                    longitude: 100
                }
            }
    
            MouseArea {
                anchors.fill: parent
                onPressed: {
                    var coord = map.toCoordinate(Qt.point(mouse.x,mouse.y))
                    console.log("coordinate: " + coord)
    
                    //MyScript.createSpriteObjects(map.toCoordinate(Qt.point(mouse.x,mouse.y)).latitude,map.toCoordinate(Qt.point(mouse.x,mouse.y)).longitude)
                    MyScript.createSpriteObjects(mouse.x,mouse.y)
    
                }
            }
        }
    
    
    }
    
    

    componentCreation.js

    var component
    var sprite
    
    function createSpriteObjects(posX,posY) {
        component = Qt.createComponent("Sprite.qml");
        if (component.status == Component.Ready)
            finishCreation(posX,posY);
        else
            component.statusChanged.connect(finishCreation);
    
    }
    
    function finishCreation(posX,posY) {
        if (component.status == Component.Ready) {
            //sprite = component.createObject(map, {"marker.coordinate": map.toCoordinate(Qt.point(posX,posY))});
            sprite = component.createObject(map);
            console.log("Object Created " + map.toCoordinate(Qt.point(posX,posY)));
            //sprite = component.createObject(appWindow);
            if (sprite == null) {
                console.log("Error creating Object");
            }
        }
        else if (component.status == Component.Error) {
            console.log("Error loading component:", component.errorString());
        }
    }
    
    

    Sprite.qml

    import QtQuick 2.0
    import QtLocation 5.3
    
    
    MapQuickItem {
        id: marker
        sourceItem: Image {
            id: image
            source: "marker.png"
        }
        coordinate {
            latitude: 5
            longitude: 100
        }
        anchorPoint.x: image.width / 2
        anchorPoint.y: image.height / 2
        visible: true
    }
    
    /*Rectangle {
        width: 100
        height: 20
        x: 10
        y:10
    }*/
    
    
    K O 2 Replies Last reply 4 Jul 2017, 16:35
    0
    • M mkhtrmn
      4 Jul 2017, 15:05

      Hello,

      I want to create an application which allow me to draw polyline when I click two points somewhere on the map. Something like googlemap.

      I managed to draw one polyline, but how to create more than one polyline?
      main.qml

      import QtQuick 2.6
      import QtQuick.Window 2.2
      import "componentCreation.js" as MyScript
      import QtQuick.Controls 2.1
      import QtLocation 5.3
      import QtPositioning 5.2
      
      Window {
          id: appWindow
          width: 512
          height: 512
          visible: true
          Map {
              id: map
              //width: win.width - kolom.width - row1.spacing
              anchors.fill: parent
              activeMapType: map.supportedMapTypes[2]
              zoomLevel: 1
              //z:1
      
      
              center {
                  latitude: 5
                  longitude: 100
              }
              plugin: Plugin {
                  name: 'osm';
                  PluginParameter {
                      name: 'osm.mapping.offline.directory';
                      value: ':/offline_tiles/'
                  }
              }
              MapCircle {
                  radius: 800000
                  color: 'blue'
                  center {
                      latitude: 5
                      longitude: 100
                  }
              }
      
              MouseArea {
                  anchors.fill: parent
                  onPressed: {
                      var coord = map.toCoordinate(Qt.point(mouse.x,mouse.y))
                      console.log("coordinate: " + coord)
      
                      //MyScript.createSpriteObjects(map.toCoordinate(Qt.point(mouse.x,mouse.y)).latitude,map.toCoordinate(Qt.point(mouse.x,mouse.y)).longitude)
                      MyScript.createSpriteObjects(mouse.x,mouse.y)
      
                  }
              }
          }
      
      
      }
      
      

      componentCreation.js

      var component
      var sprite
      
      function createSpriteObjects(posX,posY) {
          component = Qt.createComponent("Sprite.qml");
          if (component.status == Component.Ready)
              finishCreation(posX,posY);
          else
              component.statusChanged.connect(finishCreation);
      
      }
      
      function finishCreation(posX,posY) {
          if (component.status == Component.Ready) {
              //sprite = component.createObject(map, {"marker.coordinate": map.toCoordinate(Qt.point(posX,posY))});
              sprite = component.createObject(map);
              console.log("Object Created " + map.toCoordinate(Qt.point(posX,posY)));
              //sprite = component.createObject(appWindow);
              if (sprite == null) {
                  console.log("Error creating Object");
              }
          }
          else if (component.status == Component.Error) {
              console.log("Error loading component:", component.errorString());
          }
      }
      
      

      Sprite.qml

      import QtQuick 2.0
      import QtLocation 5.3
      
      
      MapQuickItem {
          id: marker
          sourceItem: Image {
              id: image
              source: "marker.png"
          }
          coordinate {
              latitude: 5
              longitude: 100
          }
          anchorPoint.x: image.width / 2
          anchorPoint.y: image.height / 2
          visible: true
      }
      
      /*Rectangle {
          width: 100
          height: 20
          x: 10
          y:10
      }*/
      
      
      K Offline
      K Offline
      koahnig
      wrote on 4 Jul 2017, 16:35 last edited by
      #2

      @mkhtrmn

      Probably you need to show the source code.

      Crystall ball reading tells me that you are deleting or refreshing some view while preparing for the next polyline.

      Vote the answer(s) that helped you to solve your issue(s)

      M 1 Reply Last reply 6 Jul 2017, 08:46
      0
      • M mkhtrmn
        4 Jul 2017, 15:05

        Hello,

        I want to create an application which allow me to draw polyline when I click two points somewhere on the map. Something like googlemap.

        I managed to draw one polyline, but how to create more than one polyline?
        main.qml

        import QtQuick 2.6
        import QtQuick.Window 2.2
        import "componentCreation.js" as MyScript
        import QtQuick.Controls 2.1
        import QtLocation 5.3
        import QtPositioning 5.2
        
        Window {
            id: appWindow
            width: 512
            height: 512
            visible: true
            Map {
                id: map
                //width: win.width - kolom.width - row1.spacing
                anchors.fill: parent
                activeMapType: map.supportedMapTypes[2]
                zoomLevel: 1
                //z:1
        
        
                center {
                    latitude: 5
                    longitude: 100
                }
                plugin: Plugin {
                    name: 'osm';
                    PluginParameter {
                        name: 'osm.mapping.offline.directory';
                        value: ':/offline_tiles/'
                    }
                }
                MapCircle {
                    radius: 800000
                    color: 'blue'
                    center {
                        latitude: 5
                        longitude: 100
                    }
                }
        
                MouseArea {
                    anchors.fill: parent
                    onPressed: {
                        var coord = map.toCoordinate(Qt.point(mouse.x,mouse.y))
                        console.log("coordinate: " + coord)
        
                        //MyScript.createSpriteObjects(map.toCoordinate(Qt.point(mouse.x,mouse.y)).latitude,map.toCoordinate(Qt.point(mouse.x,mouse.y)).longitude)
                        MyScript.createSpriteObjects(mouse.x,mouse.y)
        
                    }
                }
            }
        
        
        }
        
        

        componentCreation.js

        var component
        var sprite
        
        function createSpriteObjects(posX,posY) {
            component = Qt.createComponent("Sprite.qml");
            if (component.status == Component.Ready)
                finishCreation(posX,posY);
            else
                component.statusChanged.connect(finishCreation);
        
        }
        
        function finishCreation(posX,posY) {
            if (component.status == Component.Ready) {
                //sprite = component.createObject(map, {"marker.coordinate": map.toCoordinate(Qt.point(posX,posY))});
                sprite = component.createObject(map);
                console.log("Object Created " + map.toCoordinate(Qt.point(posX,posY)));
                //sprite = component.createObject(appWindow);
                if (sprite == null) {
                    console.log("Error creating Object");
                }
            }
            else if (component.status == Component.Error) {
                console.log("Error loading component:", component.errorString());
            }
        }
        
        

        Sprite.qml

        import QtQuick 2.0
        import QtLocation 5.3
        
        
        MapQuickItem {
            id: marker
            sourceItem: Image {
                id: image
                source: "marker.png"
            }
            coordinate {
                latitude: 5
                longitude: 100
            }
            anchorPoint.x: image.width / 2
            anchorPoint.y: image.height / 2
            visible: true
        }
        
        /*Rectangle {
            width: 100
            height: 20
            x: 10
            y:10
        }*/
        
        
        O Offline
        O Offline
        ofmrew
        wrote on 4 Jul 2017, 19:20 last edited by
        #3

        @mkhtrmn

        Try the following:
        QVector<QLineF> polyPath;
        polyPath.append(QLineF(p1p, p1));
        p.drawLines(polyPath);

        The first line creates a container for the line segments; the second add the line segments to the container; and the third draws the segments. You can add as many polylines as required, since they are treated as individual line segments.

        M 1 Reply Last reply 6 Jul 2017, 08:20
        0
        • O ofmrew
          4 Jul 2017, 19:20

          @mkhtrmn

          Try the following:
          QVector<QLineF> polyPath;
          polyPath.append(QLineF(p1p, p1));
          p.drawLines(polyPath);

          The first line creates a container for the line segments; the second add the line segments to the container; and the third draws the segments. You can add as many polylines as required, since they are treated as individual line segments.

          M Offline
          M Offline
          mkhtrmn
          wrote on 6 Jul 2017, 08:20 last edited by
          #4

          @ofmrew said in Dynamic Polyline in Qt Location/Map:

          @mkhtrmn

          Try the following:
          QVector<QLineF> polyPath;
          polyPath.append(QLineF(p1p, p1));
          p.drawLines(polyPath);

          The first line creates a container for the line segments; the second add the line segments to the container; and the third draws the segments. You can add as many polylines as required, since they are treated as individual line segments.

          Thanks for the recommendation. Actually, I am working on QML and I use MapPolyline to draw on the map.

          I have made a progress, I can create a rectangle dynamicaly using QML dynamic object creation. But, when I tried to draw a MapQuickItem or MapCircle it shows nothing.

          *I have updated my post with my code

          1 Reply Last reply
          0
          • K koahnig
            4 Jul 2017, 16:35

            @mkhtrmn

            Probably you need to show the source code.

            Crystall ball reading tells me that you are deleting or refreshing some view while preparing for the next polyline.

            M Offline
            M Offline
            mkhtrmn
            wrote on 6 Jul 2017, 08:46 last edited by
            #5

            @koahnig I have updated my post by adding the source code

            K 1 Reply Last reply 6 Jul 2017, 08:59
            0
            • M mkhtrmn
              6 Jul 2017, 08:46

              @koahnig I have updated my post by adding the source code

              K Offline
              K Offline
              koahnig
              wrote on 6 Jul 2017, 08:59 last edited by
              #6

              @mkhtrmn

              Sorry, QML is not my field of expertize at all.

              My guess is that you are displaying something with a local instance and therefore, it is vanishing for the next trial. However, with QML there is no chance that I can help.

              I will move your post to QML forum.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • oria66O Offline
                oria66O Offline
                oria66
                wrote on 6 Apr 2018, 01:02 last edited by
                #7

                So, this post is old relatively. However, it is marked as UNSOLVED. Anyway, there is someone that can be helpful.

                This is the funcionality asked: dynamic polyline with two modes. One mode is continous polilyne, the other is an array of single lines. The code speak by itself.

                main.qml

                ApplicationWindow {
                    id: applicationWindow
                    visible: true
                    width: 640
                    height: 480
                
                    // This property indicates the mode, if changeMode=0 is a polyline,
                    // if changeMode=OtherNumber is an array or single lines
                    property int changeMode: 1
                
                    Plugin {
                        id: osmMapPlugin
                        name: "osm"
                    }
                
                    Map {
                        id: themap
                        anchors.fill: parent
                        zoomLevel: 12
                        plugin: osmMapPlugin
                        center: QtPositioning.coordinate(-34.171634, -70.734406)
                    }
                
                    MouseArea{
                        anchors.fill: parent
                        z:1
                        onClicked: {
                            MyScript.createElements(Qt.point(mouse.x,mouse.y))
                        }
                    }
                }
                

                Line.qml

                MapPolyline {
                
                    property alias mainPolyline: mainPolyline
                
                    id: mainPolyline
                    line.width: 3
                    line.color: 'blue'
                }
                

                Marker.qml

                MapQuickItem {
                
                    property alias marker: marker
                
                    id: marker
                    sourceItem: Rectangle {
                        width: 10
                        height: 10
                        color: "red"
                        smooth: true
                        radius: 15
                    }
                    opacity: 1.0
                    anchorPoint: Qt.point(sourceItem.width/2, sourceItem.height/2)
                }
                

                componentCreation.js (import as MyScript)

                var arrayLines=[]
                var lineComplete=false
                
                function createElements(point) {
                
                    var componentMarker = Qt.createComponent("Marker.qml");
                
                    if (componentMarker.status === Component.Ready) {
                        var markerFirstCorner = componentMarker.createObject(themap);
                        markerFirstCorner.coordinate = themap.toCoordinate(point)
                
                        themap.addMapItem(markerFirstCorner)
                    }else{
                        console.log("Marker not created")
                    }
                
                    var theLine
                
                    if(changeMode===0){
                
                        //Polyline mode
                        if(arrayLines.length===0){
                            createLine(point)
                        }else{
                            theLine = arrayLines[arrayLines.length-1]
                
                            theLine.mainPolyline.addCoordinate(themap.toCoordinate(point))
                        }
                
                    }else{
                        //Array of lines
                
                        if(arrayLines.length===0 || !lineComplete){
                            createLine(point)
                            lineComplete=true
                        }else{
                            theLine = arrayLines[arrayLines.length-1]
                
                            theLine.mainPolyline.addCoordinate(themap.toCoordinate(point))
                
                            lineComplete=false
                        }
                    }
                }
                
                function createLine(point){
                
                    var componentLine = Qt.createComponent("Line.qml")
                
                    if (componentLine.status === Component.Ready) {
                        var lineFirstCorner = componentLine.createObject(themap);
                        lineFirstCorner.mainPolyline.addCoordinate(themap.toCoordinate(point))
                
                        themap.addMapItem(lineFirstCorner)
                        arrayLines.push(lineFirstCorner)
                    }else{
                        console.log("Line not created")
                    }
                }
                

                The truth is out there

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved