qtqmlmapview

Qt QML: MapPolyline not shown on the map


I'm trying to add a MapPolyLine to MapView component by following the example given in the Qt documentation here. I'm using Qt 6.7.1.

When MapPolyLine is inside the Map component, the line appears on the map, but if I change the parent component of MapPolyLine from Map to MapView, the polyline doesn't appear on the Map. I'm unable to understand this behavior of MapView. There is no example in Qt docs also demonstrating using MapPolyLine with MapView.

What changes I need to make for the MapPolyLine to work with MapView?

Here is the complete QML program demonstrating the issue:

import QtQuick
import QtPositioning
import QtLocation

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Map View")

    // MapPolyline works when used inside Map component
    Map {
        id: mapView
        anchors.fill: parent
        center: QtPositioning.coordinate(-27.5, 153.0)
        plugin: Plugin {
            id: osmPlugin
            name: "osm"
        }

        MapPolyline {
            line.width: 3
            line.color: 'green'
            path: [
                { latitude: -27, longitude: 153.0 },
                { latitude: -27, longitude: 154.1 },
                { latitude: -28, longitude: 153.5 },
                { latitude: -29, longitude: 153.5 }
            ]
        }
    }

    /////////   Changing the Map to MapView causes the line to disappear //////
    // MapView {
    //     id: mapView
    //     anchors.fill: parent

    //     map {
    //         center: QtPositioning.coordinate(-27.5, 153.0)
    //         zoomLevel: 12
    //         plugin: Plugin {
    //             id: osmPlugin
    //             name: "osm"
    //         }
    //     }

    //     MapPolyline {
    //         line.width: 3
    //         line.color: 'green'
    //         path: [
    //             { latitude: -27, longitude: 153.0 },
    //             { latitude: -27, longitude: 154.1 },
    //             { latitude: -28, longitude: 153.5 },
    //             { latitude: -29, longitude: 153.5 }
    //         ]
    //     }
    // }
}

Solution

  • There is an example of using MapView etc, you can find it here: https://doc.qt.io/qt-6/qtlocation-mapviewer-example.html

    In general, you can use dynamic creating, like this:

    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        MapView {
            anchors.fill: parent
            map {
                id: mapView
                center: QtPositioning.coordinate(-27.5, 153.0)
                plugin: Plugin {
                    id: osmPlugin
                    name: "osm"
                }
            }
    
            Component.onCompleted: {
                var item = polyline.createObject(mapView);
                mapView.addMapItem(item);
            }
        }
    
        Component {
            id: polyline
            MapPolyline {
                line.width: 3
                line.color: 'green'
                path: [
                    { latitude: -27, longitude: 153.0 },
                    { latitude: -27, longitude: 154.1 },
                    { latitude: -28, longitude: 153.5 },
                    { latitude: -29, longitude: 153.5 }
                ]
            }
        }
    }