【问题标题】:Is it possible to create a map grid in qml?是否可以在 qml 中创建地图网格?
【发布时间】:2018-03-20 08:25:29
【问题描述】:

我正在使用qml 开发我的pyqt5 应用程序,并希望将地图网格添加到我的地图中。但我有点不知道从哪里开始。那么是否有可能制作一个,如果是的话,是否有一个小例子或类似的东西让我开始研究?这将是我的map.qml

import QtQuick 2.0
import QtQuick.Controls 2.2
import QtQuick.Window 2.0 
import QtLocation 5.9
import QtPositioning 5.5


Item {
id: myItem

Plugin {
    id: mapPlugin
    name: "mapboxgl"
}

Map {
    id: map
    objectName: "mapboxgl"
    property double lat: 47.368649
    property double lon: 8.5391825
    visible: true
    anchors.fill: parent
    plugin: mapPlugin
    center {
        latitude: lat
        longitude: lon
    }
    zoomLevel: 14

    ListView {
        height: 1
        model: map
        delegate: Text {
            text: "Latitude: " + (center.latitude).toFixed(3) + " Longitude: " + (center.longitude).toFixed(3)
            }
        }

        MouseArea{
            id: mouseArea
            property var positionRoot: map.toCoordinate(Qt.point(mouseX, mouseY))
            anchors.fill: parent
            onClicked: {
                    var component = Qt.createComponent("addAttribute.qml")
                    if (component.status === Component.Ready) {
                    var dialog = component.createObject(parent,{popupType: 1})
                    dialog.sqlPosition = positionRoot
                    dialog.show()
               }
            }
        }

    MapQuickItem {
        id: marker
        objectName: "marker"
        visible: false
        anchorPoint.x: 0.5 * image.width
        anchorPoint.y: image.height
        sourceItem: Image {
            id: image
            source: "icons/markerIcon.png"
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    ToolTip.timeout = 2000
                    ToolTip.visible = true
                    ToolTip.text = qsTr("Coordinates: %1, %2").arg(marker.coordinate.latitude).arg(marker.coordinate.longitude)
                }
            }
        }
    }

    MapItemView {
        model: markerModel
        delegate: MapQuickItem{
            anchorPoint: Qt.point(2.5, 2.5)
            coordinate: QtPositioning.coordinate(markerPosition.x, markerPosition.y)
            zoomLevel: 0
            sourceItem: Column{
                    Image {
                        id: imag
                        source: "icons/markerIcon.png"
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {

                                ToolTip.timeout = 2000
                                ToolTip.visible = true
                                ToolTip.text = qsTr("Coordinates: %1, %2".arg(markerPosition.x).arg(markerPosition.y))
                            }
                        }
                    }

                    Text {
                        text: markerTitle
                        font.bold: true
                    }
            }
        }
    }
    MapParameter {
    type: "source"
    property var name: "coordinates"
    property var sourceType: "geojson"
    property var data: '{ "type": "FeatureCollection", "features": \
        [{ "type": "Feature", "properties": {}, "geometry": { \
        "type": "LineString", "coordinates": [[ 8.541484, \
        47.366850 ], [8.542171, 47.370018],[8.545561, 47.369233]]}}]}'
    }

    MapParameter {
        type: "layer"
        property var name: "layer"
        property var layerType: "line"
        property var source: "coordinates"
        property var before: "road-label-small"
    }

    MapParameter {
        objectName: "paint"
        type: "paint"
        property var layer: "layer"
        property var lineColor: "black"
        property var lineWidth: 8.0
    }

    MapParameter {
        type: "layout"
        property var layer: "layer"
        property var lineJoin: "round"
        property var lineCap: "round"
    }
}
}

网格可能看起来像这样。

【问题讨论】:

  • 答案是:可能是的。为了消除可能,我们需要更多信息。你能否提供一些你已经拥有的的代码,并更详细地解释你喜欢什么样的地图网格?也许你可以添加一些图像来说明它现在的样子,以及它应该是什么样子。 (还有更详细的功能描述
  • 我添加了我的qml 代码和网格外观的图像。目前我没有网格,因为我不知道如何制作一个。这就是为什么我的网格没有任何图像的原因。

标签: python pyqt qml pyqt5


【解决方案1】:

要绘制网格,请使用Canvas,如下所示

import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.5
import QtPositioning 5.5

Window {
    visible: true
    title: "Python OSM"
    width: 640
    height: 480
    Map {
        id: map
        anchors.fill: parent
        plugin: Plugin {
            name: "osm"
        }
        center: QtPositioning.coordinate(-12.0464, -77.0428)
        zoomLevel: 14
    }
    Canvas {
        id: root
        anchors.fill : parent
        property int wgrid: 20
        onPaint: {
            var ctx = getContext("2d")
            ctx.lineWidth = 1
            ctx.strokeStyle = "black"
            ctx.beginPath()
            var nrows = height/wgrid;
            for(var i=0; i < nrows+1; i++){
                ctx.moveTo(0, wgrid*i);
                ctx.lineTo(width, wgrid*i);
            }

            var ncols = width/wgrid
            for(var j=0; j < ncols+1; j++){
                ctx.moveTo(wgrid*j, 0);
                ctx.lineTo(wgrid*j, height);
            }
            ctx.closePath()
            ctx.stroke()
        }
    }
}

【讨论】:

  • 你能解释一下你在beginPath之后做什么吗?
  • 他计算行数,然后在for-loop中他首先跳到每行的开头,然后从那里画线(到线的端点)
  • @Blinxen 确切地说,正如 derM 所说,我计算每条线的初始坐标和最终坐标,然后绘制它们。 moveTo 是一个未绘制的跳转,而不是 lineTo 绘制一条线。
猜你喜欢
  • 1970-01-01
  • 2018-03-07
  • 2011-11-26
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多