繁体   English   中英

在QT、QML、C++中清除路径和获取路径点

[英]Clearing route and getting way points in QT,QML,C++

我有两个以某种方式相关的问题。

  1. 我在开放的街道地图上创建了一条路线,我想提取与生成路线的路点相对应的点列表(不仅仅是起点和终点)。 如何实现? 例如,我想从下面的图像中提取生成的红色路线的路点(当然我不想从路线中提取所有点,而是从 10 米中的 10 个中提取)。

在此处输入图像描述

  1. 我如何用红色擦除生成的路线,并拥有原始地图(没有红色路线) 我已经在地图项目上尝试了很多功能,但没有一个有效。 例如,我尝试了下面的代码,但红色路线仍然存在。

     function clearMapDataForSession() { mapview.clearData(); routeModel.update() }

您可以使用属性pathsegmentsRoute获取坐标列表。 path属性直接为您提供给定Route上的coordinates列表,另一方面, segments属性为您提供 RouteSegments 列表,而RouteSegments又包含由其path属性给出的coordinates列表。

通过segments打印Route坐标列表:

var segments = routeModel.get(0).segments
for (var i = 0; i < segments.length; i++) {
    var path = segments[i].path
    for (var j = 0; j < path.length; j++)
        console.log(path[j])
}

通过path打印Route坐标列表:

var path = routeModel.get(0).path
for (var i = 0; i < path.length; i++) {
    console.log(path[i])
}

如果比较两个选项给出的坐标列表,它们是相同的。 RouteSegments的好处是您可以将线段的distance作为属性获取。 因此,如果您想在Route上生成相同距离的坐标/点列表,这将有助于您编写某种算法。

为了擦除生成的Route ,您需要在RouteModel上调用reset() 如果您还想清除RouteQuery的航路点,您也应该调用clearWaypoints()

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtLocation 5.15
import QtPositioning 5.15

ApplicationWindow {
    id: window
    width: 800
    height: 600
    visible: true
    title: qsTr("Map")

    header: ToolBar {
        RowLayout {
            anchors.fill: parent
            ToolButton {
                text: qsTr("Reset")
                onClicked: {
                    routeQuery.clearWaypoints()
                    routeModel.reset()
                }
            }
        }
    }

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    RouteQuery {
        id: routeQuery
    }

    RouteModel {
        id: routeModel
        plugin: mapPlugin
        query: routeQuery
        autoUpdate: false
    }

    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
        zoomLevel: 14

        MapItemView {
            model: routeModel
            delegate: MapRoute {
                route: routeData
                line.color: "blue"
                line.width: 5
                smooth: true
                opacity: 0.8
            }
        }

        MapItemView {
            model: routeModel.status == RouteModel.Ready ? routeModel.get(0).path : null
            delegate: MapQuickItem {
                anchorPoint.x: pathMarker.width / 2
                anchorPoint.y: pathMarker.height / 2
                coordinate: modelData

                sourceItem: Rectangle {
                    id: pathMarker
                    width: 8
                    height: 8
                    radius: 8
                    border.width: 1
                    border.color: "black"
                    color: "yellow"
                }
            }
        }

        MapItemView {
            model: routeQuery.waypoints
            delegate: MapQuickItem {
                anchorPoint.x: waypointMarker.width / 2
                anchorPoint.y: waypointMarker.height / 2
                coordinate: modelData

                sourceItem: Rectangle {
                    id: waypointMarker
                    width: 10
                    height: 10
                    radius: 10
                    border.width: 1
                    border.color: "black"
                    color: "red"
                }
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                routeQuery.addWaypoint(map.toCoordinate(Qt.point(mouse.x,mouse.y)))
                routeModel.update()
            }
        }
    }
}

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM