首页 > 解决方案 > Qt 中的地理编码模型

问题描述

我正在使用 QT/QML 编写一个小应用程序,但我的应用程序中的 GeocodeModel 有问题。我不知道,为什么它不起作用。我是用 YT 教程完成的,但它的工作原理是由人完成的。

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6

ApplicationWindow {
    id: app_window
    visible: true
    width: 1024
    height: 800
    title: qsTr("Navigation")
    PositionSource {
        active: true
        onPositionChanged: {
            map_id.center = position.coordinate;
        }
    }
    Rectangle {
        id: mapRectangleID
        width: 1024
        height: 800
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        Map {
            id: map_id
            anchors.fill: parent
            plugin: Plugin { name: "osm" }
            center: QtPositioning.coordinate(51.320729390711975,12.280097007751465)
            zoomLevel: 15
            MapQuickItem {
                //coordinate: QtPositioning.coordinate(59.91, 10.75)
                sourceItem: Image {
                    id: endPointImage
                    source: "assets/marker.png"
                    width: 40.1
                    height: 34.3
                } //size and position of maker
                anchorPoint.x: endPointImage.width / 2
                anchorPoint.y: endPointImage.height
            } //marker
            RouteModel {
                id: routeBetweenPoints
                plugin: Plugin { name: "osm" }
                query: RouteQuery {id: routeQuery }
                Component.onCompleted: {
                    routeQuery.addWaypoint(QtPositioning.coordinate(51.318784,12.2773504 ));
                    routeQuery.addWaypoint(QtPositioning.coordinate(51.3117764,12.280909000000065 ));
                    //routeQuery.addWaypoint(endPointGeaocodeModel)
                    update();
                }
            } //start and end point
            MapItemView {
                model: routeBetweenPoints
                delegate: Component {
                    MapRoute {
                        route: routeData
                        line.color: "red"
                        line.width: 10
                    }
                }
            }//linie, die beide punkte verbindet
            GeocodeModel{
                id: endPointGeaocodeModel
                plugin: Plugin { name: "osm" }
                query: "Sandakerveien 116, Oslo"
                onLocationsChanged: {
                    if (count)
                        endPointImage.coordinate = get(0).coordinate;
                }
                Component.onCompleted: update()
            } //suche den platz mit strasse und stadt
            Rectangle{
                id:_ifStartPointLongitude
                width: 100
                height: 20
                border.color: 'gray'
                border.width: 2
                x: 900
                y: 120
                anchors.left: app_window.right
                TextInput {
                    id: txtPlainStartPointLongitude
                    anchors.fill: parent
                    anchors.margins: 4
                }
            }
        } //all widgets and items of map
    } //size and position of map
}

并且此元素不起作用:

GeocodeModel{
    id: endPointGeaocodeModel
    plugin: Plugin { name: "osm" }
    query: "Sandakerveien 116, Oslo"
    onLocationsChanged: {
        if (count)
            endPointImage.coordinate = get(0).coordinate;
    }
    Component.onCompleted: update()
} //suche den platz mit strasse und stadt

我也尝试了其他城市,街道,但仍然无法正常工作。有人可以解释一下,我做错了什么/写错了吗?

标签: qtqmlqt5openstreetmapgeocode

解决方案


问题是因为item Image没有坐标属性,有坐标属性的是MapQuickItem,所以我们必须给那个item设置,所以我们必须给MapQuickItem一个id,观察图片我们会将地图的中心建立到相同的坐标。

// ...

MapQuickItem {
    id: marker_id // <---
    sourceItem: Image {
        id: endPointImage
        source: "assets/marker.png"
        width: 100
        height: 100
    } //size and position of maker
    anchorPoint.x: endPointImage.width / 2
    anchorPoint.y: endPointImage.height
} //marker

// ...

GeocodeModel{
    id: endPointGeaocodeModel
    plugin: Plugin { name: "osm" }
    query: "Sandakerveien 116, Oslo"
    onLocationsChanged: {
        if (count> 0){
            marker_id.coordinate = get(0).coordinate // <----
            map_id.center = get(0).coordinate // <----
        }
    }
    Component.onCompleted: update()
} //suche den platz mit strasse und stadt

// ...

推荐阅读