2016-10-01 100 views
1

你好,親愛的程序員,Recenter QT/QML地圖中的Qt 5

我想要做的就是回到中心我的地圖下面的QML。 我的座標是一對數字,並不像地圖視圖中的地址那樣。

ApplicationWindow { 
    visible: true 
    width: 720 
    height: 1280 
    title: qsTr("") 
    id: root 
    Map { 
     id: map 
     anchors.centerIn: parent; 
     anchors.fill: parent 
     zoomLevel: 11 
     objectName: "mainMap" 

     center { 
     id: mapCenter 
     latitude : 50.89 
     longitude: 11.23 
     } 
     plugin: Plugin { 
      name: "here" 
      PluginParameter { name: "here.app_id"; value: "R9qav4Kw6gO5XKSxNiOO" } 
      PluginParameter { name: "here.token"; value: "58UCNRCr1dZxhLL2Bmmz3Q" } 
      PluginParameter { name: "here.proxy"; value: "system" } 
     } 
     function setPosition(pos) { 
      map.toCoordinate(pos); 
      map.update(); 
     } 
} 

C++方面現在比較簡單。 這是我迄今爲止最好的拍攝,直接改變緯度和緯度似乎從來沒有奏效。早期版本中的工作是創建一個新的對象作爲座標,然後將其提供給地圖。

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include <QQmlContext> 
#include <QObject> 
#include <QTime> 
#include <QBasicTimer> 
#include <QDebug> 
#include <QEasingCurve> 
#include <QGeoCoordinate> 
#include <QtPositioning/private/qgeoprojection_p.h> 
#include <QGeoServiceProvider> 
#include <QDebug> 
#include <QNetworkRequest> 

int main(int argc, char *argv[]) 
{ 
    QGuiApplication app(argc, argv); 
    QUrl data("https://lstsim.de/js/dispatch/1.js"); 
    QNetworkRequest request(data); 

    QQmlApplicationEngine engine; 
    engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 
    QObject *rootObject = engine.rootObjects().first(); 
    QObject* mainMapCenter = rootObject->findChild<QObject*>("mainMap"); 
    if(mainMapCenter != NULL){ 
     QVariant returnedValue; 
     QPoint point(12,12); 
     QMetaObject::invokeMethod(mainMapCenter, "setPosition", 
     Q_RETURN_ARG(QVariant, returnedValue), 
     Q_ARG(QVariant, point)); 
     qDebug() << "found map"; 
    } 
    return app.exec(); 
} 

像在早期版本中提到befor這工作:

function centermyposition(){ //sets my position, but only once (do not update automatically) 
     var coord = Qt.createQmlObject('import QtMobility.location 1.1; Coordinate{latitude:' + positionSource.position.coordinate.latitude + ';longitude:' + positionSource.position.coordinate.longitude + ';}', positionSource, "coord"); 
     map.center = coord; 
     myMapRoot.updateViewport() 
} 

回答

1

我發現,我工作的解決方案:

ApplicationWindow { 

Location { 
     id: mapCentre 
     coordinate { 
      latitude: -27.5 
      longitude: 153.1 
     } 
    } 
    Map { 
     id: map 
     anchors.centerIn: parent; 
     anchors.fill: parent 
     zoomLevel: 11 
     objectName: "mainMap" 

     function recenter(lat,lng) { 
      mapCentre.coordinate.latitude = lat 
      mapCentre.coordinate.longitude = lng 
     } 
    } 
}