QML地图仅垂直缩放

QML地图仅垂直缩放

本文介绍了QML地图仅垂直缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt中的地图是否具有属性,因此如果我将鼠标光标移动到地图上的随机位置并尝试使用鼠标滚轮更改缩放级别,它不会移动中心吗?因为默认情况下,地图会缩放,但是会将现有的中心移向鼠标光标位置(下图示例;如果光标位于显示的位置上,并且我们尝试更改缩放比例,则地理标记图片将沿箭头方向移动).我只需要它从地图的指定中心直接向上缩放即可,而无需考虑鼠标光标的位置.

地图说明:

解决方案

可能应该有更优雅的解决方案,但这是简化的代码:

 导入QtQuick 2.0导入QtQuick.Window 2.0导入QtLocation 5.6导入QtPositioning 5.6窗户 {宽度:512高度:512可见:正确插入 {id:mapPluginPluginParameter {名称:"osm.useragent";值:用户地图"}名称:"esri"}地点 {//定义将成为地图中心"的位置id:mapCenter协调 {纬度:45.525180446经度:13.5575992170}}地图 {ID:地图anchors.fill:父级插件:mapPlugin中心:mapCenter.coordinateonCenterChanged:{//地图中心一旦更改-我们将检查坐标是否与我们的坐标不同//mapCenter坐标;如果是,则将地图中心坐标设置为等于mapCenter如果(map.center!= mapCenter.coordinate){map.center = mapCenter.coordinate}}MapCircle {//圈出以检查其是否正确缩放中心:mapCenter.coordinate半径:500.0颜色:绿色"border.width:3}}} 

此方法的主要思想是控制地图 center 的每次更改.万一发生变化-检查天气是否等于我们的中心.如果它不等于我们想要的中心 mapCenter -我们将其更改.

结果:

Does map in Qt have a property, so it doesn't move center if I move mouse cursor on random place on map and try to change zoom level with mouse wheel? Because on default, map zooms, but moves existing center towards mouse cursor location (example on picture below; if cursor is on shown position and we try to change zoom, geotag picture will move in arrow direction). I just need it to zoom straight up from designated center of map, regardless of mouse cursor location.

Map description:https://doc-snapshots.qt.io/qt5-5.9/qml-qtlocation-map.html

QML code

import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6

Item{
    id: itemControl
    objectName: "itemControl"
    width: 512
    height: 512
    visible: true

Plugin {
    id: mapPlugin
    PluginParameter { name: "osm.useragent"; value: "usermap" }
    name: "esri"
}

Map {
    id:map
    gesture.enabled: true
    gesture.acceptedGestures: MapGestureArea.PinchGesture | MapGestureArea.PanGesture
    objectName: "map"
    anchors.fill: parent
    bearing: 0
    width: 512
    height: 512
    plugin: mapPlugin
    center {
        latitude: 45.525180446
        longitude: 13.5575992170
    }
    zoomLevel: 15

    //If user doubleclicks on map update coordinates of pixel to coordinates on UI
    signal qmlSignalUpdateLat(string msg)
    signal qmlSignalUpdateLon(string msg)

    MouseArea
    {
        id: mousearea
        hoverEnabled: true
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton
        onDoubleClicked: {
            map.center.latitude = map.toCoordinate(Qt.point(mouseX,mouseY)).latitude
            map.center.longitude = map.toCoordinate(Qt.point(mouseX,mouseY)).longitude
            map.qmlSignalUpdateLat(map.center.latitude)
            map.qmlSignalUpdateLon(map.center.longitude)
        }
    }

    function updateMap(lat,lon,bear){
        map.center.latitude = lat;
        map.center.longtitude = lon;
        map.bearing = bear;
    }

}
}
解决方案

Probably there should be more elegant solutions, but here is your code simplified:

import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6

Window {
    width: 512
    height: 512
    visible: true
    Plugin {
        id: mapPlugin
        PluginParameter { name: "osm.useragent"; value: "usermap" }
        name: "esri"
    }
    Location {
        // Define location that will be "center" of map
        id: mapCenter
        coordinate {
            latitude: 45.525180446
            longitude: 13.5575992170
        }
    }
    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: mapCenter.coordinate

        onCenterChanged: {
            // As soon as map center changed -- we'll check if coordinates differs from our
            //   mapCenter coordinates and if so, set map center coordinates equal to mapCenter
            if (map.center != mapCenter.coordinate) {
                map.center = mapCenter.coordinate
            }
        }

        MapCircle {
            // Circle just for ability to check if it zooms correctly
            center: mapCenter.coordinate
            radius: 500.0
            color: "green"
            border.width: 3
        }
    }
}

Main idea behind this is to take control over each change of map's center. In case if it changes -- check weather it equal to our center. If it is not equal to our desired center mapCenter -- we change it.

As a result:

这篇关于QML地图仅垂直缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:03