QML地图可见区域 [英] QML Map visible region

查看:397
本文介绍了QML地图可见区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在使用QtLocation来显示地图.由于只有QML API可以呈现地图,因此这是我的QML文件:

In my application, I am using QtLocation to display a map. Since there is only QML API to render the map, here is my QML file:

import QtQuick 2.0
import QtPositioning 5.5
import QtLocation 5.5

Item{
    anchors.fill: parent

    Plugin{
        id: osmplugin
        name: "osm"
    }

    Map {
        anchors.fill: parent
        id: map
        plugin: osmplugin;
        zoomLevel: (maximumZoomLevel - minimumZoomLevel)/2
        center {
            // The Qt Company in Oslo
            latitude: 59.9485
            longitude: 10.7686
        }
    }

    function bbox(){
        return map.visibleRegion;
    }
}

在C ++代码中,我需要知道地图小部件中的当前可见区域QML Map具有属性visibleRegion

In the C++ code, I need to know the currently visible region in the map widget, QML Map has the property visibleRegion http://doc.qt.io/qt-5/qml-qtlocation-map.html#visibleRegion-prop

但是我不明白如何从C ++代码中获取它,因为QGeoShape是抽象的;

But I don't understand how to get it from C++ code, since QGeoShape is abstract;

我尝试过:

    QQuickItem* map = mMap->rootObject();
    QGeoRectangle rect;
    bool ok = QMetaObject::invokeMethod( map, "bbox",  Qt::DirectConnection, Q_RETURN_ARG( QGeoRectangle, rect ) );
    if ( !ok )
        qDebug() << " Shit happens!";

    qDebug() << rect.isValid();

但这没有帮助.请告诉我如何从QML Map中获得可见的矩形.

But it did not help. Please tell me how do I get visible rectangle from QML Map.

推荐答案

正确的语法是:

QQuickItem* map = mMap->rootObject();
QVariant ret;
bool ok = QMetaObject::invokeMethod( map, "bbox",  Qt::DirectConnection, Q_RETURN_ARG( QVariant, ret ) );
if ( !ok ){
    qWarning( "Fail to call qml method" );
}
QGeoRectangle rect = qvariant_cast<QGeoRectangle>( ret );
mNorth = rect.topLeft().latitude();
mSouth = rect.bottomLeft().latitude();
mWest  = rect.topLeft().longitude();
mEast  = rect.topRight().longitude();

这篇关于QML地图可见区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆