Difference between revisions of "Projects/Marble/Runners/LoadingKML"

Jump to: navigation, search
(Loading KML files into a Marble Widget)
(Loading KML files into a Marble Widget)
Line 18: Line 18:
 
#include <QtCore/QFileInfo>
 
#include <QtCore/QFileInfo>
 
#include <QtGui/QApplication>
 
#include <QtGui/QApplication>
#include <QtGui/QTreeView>
 
 
   
 
   
 
#include <marble/MarbleWidget.h>
 
#include <marble/MarbleWidget.h>
 
#include <marble/MarbleModel.h>
 
#include <marble/MarbleModel.h>
 
#include <marble/MarbleRunnerManager.h>
 
#include <marble/MarbleRunnerManager.h>
#include <marble/GeoDataTreeModel.h>
 
 
   
 
   
 
using namespace Marble;
 
using namespace Marble;
Line 40: Line 38:
 
     MarbleRunnerManager* manager = new MarbleRunnerManager( model->pluginManager() );
 
     MarbleRunnerManager* manager = new MarbleRunnerManager( model->pluginManager() );
 
   
 
   
    GeoDataDocument* document = manager->openFile( inputFile.absoluteFilePath() );
 
 
 
     MarbleWidget *mapWidget = new MarbleWidget();
 
     MarbleWidget *mapWidget = new MarbleWidget();
 
     mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
 
     mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
  
     if ( document ) {
+
     mapWidget->centerOn( GeoDataCoordinates( 26.0783, 44.4671, 0, GeoDataCoordinates::Degree ) );
mapWidget->model()->treeModel()->addDocument( document );
+
    mapWidget->zoomView( 2200 );
mapWidget->centerOn( GeoDataCoordinates( 26.0783, 44.4671, 0, GeoDataCoordinates::Degree ) );
+
 
mapWidget->zoomView( 2200 );
+
    mapWidget->model()->addGeoDataFile( inputFile.absoluteFilePath() );
 
 
mapWidget->show();
+
    mapWidget->show();
    } else {
+
        qDebug() << "Unable to open " << inputFile.absoluteFilePath();
+
    }
+
 
   
 
   
 
     return app.exec();
 
     return app.exec();
}
+
}</source>
</source>
+
  
 
Copy and paste the code above into a text editor. Then save it as <tt>my_marble.cpp</tt> and compile it by entering the following command on the command line:
 
Copy and paste the code above into a text editor. Then save it as <tt>my_marble.cpp</tt> and compile it by entering the following command on the command line:

Revision as of 19:35, 22 June 2012


Editing Projects/Marble/MarbleCPlusPlus

Search
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 8 - Reverse Geocoding
What's Next   Tutorial 10 - Opening .kml, .gpx, ... files
Further Reading   n/a

Loading KML files into a Marble Widget

Marble uses so-called runners to calculate routes, do reverse geocoding, parse files and search for placemarks (cities, addresses, points of interest, ...). This tutorial shows how to use the MarbleRunnerManager class to open a .kml (or .gpx, .osm, ...) file and display it into the Marble Widget.

#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtGui/QApplication>
 
#include <marble/MarbleWidget.h>
#include <marble/MarbleModel.h>
#include <marble/MarbleRunnerManager.h>
 
using namespace Marble;
 
int main(int argc, char** argv)
{
    QApplication app(argc,argv);
 
    QFileInfo inputFile( app.arguments().last() );
    if ( app.arguments().size() < 2 || !inputFile.exists() ) {
        qWarning() << "Usage: " << app.arguments().first() << "file.kml";
        return 1;
    }
 
    MarbleModel *model = new MarbleModel;
    MarbleRunnerManager* manager = new MarbleRunnerManager( model->pluginManager() );
 
    MarbleWidget *mapWidget = new MarbleWidget();
    mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
 
    mapWidget->centerOn( GeoDataCoordinates( 26.0783, 44.4671, 0, GeoDataCoordinates::Degree ) );
    mapWidget->zoomView( 2200 );
 
    mapWidget->model()->addGeoDataFile( inputFile.absoluteFilePath() );
 
    mapWidget->show();
 
    return app.exec();
}

Copy and paste the code above into a text editor. Then save it as my_marble.cpp and compile it by entering the following command on the command line:

 g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui -lQtCore

If things go fine, execute ./my_marble some-file.kml and you get a Marble Widget which displays your KML file. For example, download and unpack Bucharest.kml (a LinearRing representing Bucharest's city boundaries), place it in the same folder as your my_marble.cpp file and run ./my_marble bucharest.kml. The result should be similar to this:

LoadKML.png

noframe
 
Note
The same method works for loading other file types too, like OpenStreetMap (.osm), GPX and Shapefiles (.shp).


noframe
 
Note
If you provide maps in your application please check the Terms of Use of the map material. The map material that is shipped with Marble is licensed in the spirit of Free Software. This usually means at least that the authors should be credited and that the license is mentioned. E.g. for OpenStreetMap the license is CC-BY-SA. Other map data shipped with Marble is either public domain or licensed in the spirit of the BSD license.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal