1

我有 Qt Qml 应用程序,我需要在其中显示带有两个 MapQuickItems 的地图。一个是出租车另一个是客户。我想在地图中显示它们。我希望地图在出租车接近客户时自动放大或缩小。不涉及路由或手势。用户不应与地图交互。

我试图玩弄 map.center 属性。但是当出租车很远时,它就不好用了。

import QtQuick 2.12
import QtQuick.Window 2.12
import QtPositioning 5.12
import QtLocation 5.5

Rectangle
{
    id: mapWindow
    visible: false
    property real taxiLatitude: 0
    property real taxiLongitude: 0
    property real customerLatitude: 0
    property real customerLongitude: 0

    Plugin
    {
        id: googleMap
        name: "googlemaps"
    }

    Map
    {
        id: map
        anchors.fill: mapWindow
        plugin: googleMap
        center: QtPositioning.coordinate(taxiLatitude, taxiLongitude) //positionSource.position.coordinate
        zoomLevel: 17
        copyrightsVisible: true

        MapQuickItem
        {
            id: markerTaxi
            anchorPoint.x: imageHuman.width/4
            anchorPoint.y: imageHuman.height
            coordinate: QtPositioning.coordinate(customerLatitude, taxiLongitude)
            sourceItem: Image
            {
                id: imageHuman
                width: 40
                height: 40
                source: "qrc:/Images/extrapics/humanIcon.png"
            }
        }

        MapQuickItem
        {
            id: markerCustomer
            anchorPoint.x: image.width/4
            anchorPoint.y: image.height
            coordinate: QtPositioning.coordinate(taxiLatitude, taxiLongitude)
            sourceItem: Image
            {
                id: image
                width: 40
                height: 40
                source: "qrc:/Images/extrapics/point.png"
            }
        }
    }
}

我需要将出租车和客户都放在地图内,并且地图应该在出租车接近客户时自动放大。

我试图设置如下所示的可见区域。但它没有帮助。它显示了一个不同的地区(北美)。但是我设置的区域在完全不同的大陆上。

visibleRegion: QtPositioning.rectangle(QtPositioning.coordinate(12.921527, 75.092244), QtPositioning.coordinate(12.726949, 75.014545))
4

1 回答 1

1

使用计时器通过 fitViewportToMapItems() 更新地图

import QtQuick 2.12
import QtQuick.Window 2.12
import QtPositioning 5.12
import QtLocation 5.5

Rectangle
{
    id: mapWindow
    visible: false
    property real taxiLatitude: 0
    property real taxiLongitude: 0
    property real customerLatitude: 0
    property real customerLongitude: 0

    Plugin
    {
        id: googleMap
        name: "googlemaps"
    }

    Timer
    {
        id: mapRefreshtimer
        running: true
        interval: 2000
        repeat: true
        onTriggered:
        {
            map.fitViewportToMapItems()
        }
    }

    Map
    {
        id: map
        anchors.fill: mapWindow
        plugin: googleMap

        MapQuickItem
        {
            id: markerTaxi
            anchorPoint.x: imageHuman.width/4
            anchorPoint.y: imageHuman.height
            coordinate: QtPositioning.coordinate(customerLatitude, taxiLongitude)
            visible: true
            sourceItem: Image
            {
                id: imageHuman
                width: 40
                height: 40
                source: "qrc:/Images/extrapics/humanIcon.png"
            }
        }

        MapQuickItem
        {
            id: markerCustomer
            anchorPoint.x: image.width/4
            anchorPoint.y: image.height
            coordinate: QtPositioning.coordinate(taxiLatitude, taxiLongitude)
            visible: true
            sourceItem: Image
            {
                id: image
                width: 40
                height: 40
                source: "qrc:/Images/extrapics/point.png"
            }
        }

    }
}
于 2019-08-20T14:05:42.867 回答