首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何自动缩放Qml地图以适合两个MapQuickItems

如何自动缩放Qml地图以适合两个MapQuickItems
EN

Stack Overflow用户
提问于 2019-08-20 20:26:39
回答 1查看 764关注 0票数 1

我有Qt Qml应用程序,其中我需要显示两个MapQuickItems地图。一个是出租车,另一个是顾客。我想把它们都显示在地图里。我想让地图在出租车接近客户时自动放大或缩小。没有涉及到路由或手势。用户不应该能够与地图交互。

我试着摆弄一下map.center属性。但是当出租车很远的时候,它不能很好地工作。

代码语言:javascript
复制
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"
            }
        }
    }
}

我需要适合出租车和客户在地图和地图应该自动放大,因为出租车接近客户。

我试着像下面这样设置可见区域。但这是有帮助的。它显示了一个不同的区域(北美)。但我设置的区域是完全不同的大陆。

代码语言:javascript
复制
visibleRegion: QtPositioning.rectangle(QtPositioning.coordinate(12.921527, 75.092244), QtPositioning.coordinate(12.726949, 75.014545))
EN

回答 1

Stack Overflow用户

发布于 2019-08-20 22:05:43

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

代码语言:javascript
复制
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"
            }
        }

    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57573652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档