[Marble-devel] API and loading of KML files

Dennis Nienhüser earthwings at gentoo.org
Sat Sep 27 17:15:17 UTC 2014


Hi Tom,

you're right, the addGeoDataFile() method is a convenience method doing 
several things at once:
- load the file in some thread in the background
- when the data is extracted successfully, show it
- center the view on the bounding area of the data

Since you'd rather have synchronous loading and specify the map center 
and zoom values on your own, it's better to use different API methods 
for parsing the file and showing it: ParsingRunnerManager to load and 
GeoDataTreeModel for rendering. The modified example below does that.

Regards,
Dennis

#include <marble/GeoDataDocument.h>
#include <marble/GeoDataTreeModel.h>
#include <marble/MarbleWidget.h>
#include <marble/MarbleModel.h>
#include <marble/ParsingRunnerManager.h>

#include <QtGui/QApplication>

#include <iostream>

using namespace Marble;

int main(int argc, char** argv)
{
     if (argc < 2)
     {
       std::cerr << "Usage: " << argv[0] << " file.kml|file.kmz" << 
std::endl;
       return 0;
     }

     QApplication app(argc, argv);
     MarbleWidget *mapWidget = new MarbleWidget;

     // Open KML document
     ParsingRunnerManager parser(mapWidget->model()->pluginManager());
     GeoDataDocument* document = parser.openFile(argv[1]);
     if (!document)
     {
       std::cerr << "Failed to open " << argv[1] << std::endl;
       return 1;
     }

     // Have Marble render the KML document
     mapWidget->model()->treeModel()->addDocument(document);

     mapWidget->resize(800,600);
     mapWidget->setWindowTitle("DXCC Map");
     mapWidget->setMapThemeId("earth/bluemarble/bluemarble.dgml");
     mapWidget->setZoom(1100);
     GeoDataCoordinates const position(8.545486, 53.106076, 0.0, 
GeoDataCoordinates::Degree);
     mapWidget->centerOn(position);
     mapWidget->show();
     return app.exec();
}

Am 27.09.2014 15:51, schrieb Thomas 'Tom' Malkus:
> Hi,
> 
> I have found a solution. Could it been, that the view is reseted after
> loading the KML data and the data is loading as a own thread in the
> background?
> 
> If I add a delay of 1 second before calling show() the view is correct
> zoomed and positioned.
> 
> Is there a possibilty to get a message, if the KML data is complete 
> loaded?
> 
> #include <QtGui/QApplication>
> #include <QtCore/QFileInfo>
> #include <QtCore/QDebug>
> #include <marble/MarbleWidget.h>
> #include <marble/GeoDataDocument.h>
> #include <marble/GeoDataTreeModel.h>
> #include <marble/MarbleModel.h>
> 
> using namespace Marble;
> 
> void delay()
> {
>     QTime dieTime= QTime::currentTime().addSecs(1);
>     while( QTime::currentTime() < dieTime )
>     QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
> }
> 
> int main(int argc, char** argv)
> {
>     QApplication app(argc,argv);
>     MarbleWidget *mapWidget = new MarbleWidget();
> 
>     mapWidget->resize(800,600);
>     mapWidget->setWindowTitle("CQRLOG DXCC Map");
>     mapWidget->setMapThemeId("earth/bluemarble/bluemarble.dgml");
> 
> mapWidget->model()->addGeoDataFile("/home/tom/projects/Qt/dxcc/dxcc.kml");
> 
> 
>     delay();
> 
>     mapWidget->setZoom(1200);
>     GeoDataCoordinates
> position(8.545486,53.106076,0.0,GeoDataCoordinates::Degree);
>     mapWidget->centerOn(position);
> 
>     mapWidget->show();
>     return app.exec();
> }
> 
> Thanks,
> Tom


More information about the Marble-devel mailing list