QML Map visible region

↘锁芯ラ 提交于 2019-12-14 03:16:22

问题


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;
    }
}

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

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

I tried this:

    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();

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


回答1:


The correct syntax is:

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();


来源:https://stackoverflow.com/questions/33651748/qml-map-visible-region

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!