Marble/Runners/LoadingKML: Difference between revisions
CezarMocan (talk | contribs) (Created page with " {{Template:I18n/Language Navigation Bar|Editing Projects/Marble/MarbleCPlusPlus}} {{TutorialBrowser| series=Marble C++ Tutorial| name=Search| pre=[[Projects/Marble/Runners/Re...") |
CezarMocan (talk | contribs) |
||
Line 14: | Line 14: | ||
== Loading KML files into a Marble Widget == | == 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 <tt>MarbleRunnerManager</tt> class to open a .kml (or .gpx, .osm, ...) file and display it into the 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 <tt>[http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1MarbleRunnerManager.html MarbleRunnerManager]</tt> class to open a .kml (or .gpx, .osm, ...) file and display it into the Marble Widget. | ||
<source lang="cpp-qt"> | <source lang="cpp-qt"> | ||
Line 44: | Line 44: | ||
GeoDataDocument* document = manager->openFile( inputFile.absoluteFilePath() ); | GeoDataDocument* document = manager->openFile( inputFile.absoluteFilePath() ); | ||
MarbleWidget *mapWidget = new MarbleWidget(); | |||
mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml"); | |||
if ( document ) { | if ( document ) { |
Revision as of 07:35, 20 June 2012
Editing Projects/Marble/MarbleCPlusPlus
Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | Eesti | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenčina | Slovenščina | српски | Türkçe | Tiếng Việt | Українська | 简体中文 | 繁體中文
Tutorial Series | Marble C++ Tutorial |
Previous | Tutorial 8 - Reverse Geocoding |
What's Next | Tutorial 10 - Using the GeoPainter in order to paint GeoDataLineString objects |
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 <QtGui/QTreeView>
#include <marble/MarbleWidget.h>
#include <marble/MarbleModel.h>
#include <marble/MarbleRunnerManager.h>
#include <marble/GeoDataTreeModel.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() );
GeoDataDocument* document = manager->openFile( inputFile.absoluteFilePath() );
MarbleWidget *mapWidget = new MarbleWidget();
mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
if ( document ) {
mapWidget->model()->treeModel()->addDocument( document );
mapWidget->centerOn( GeoDataCoordinates( 26.0783, 44.4671, 0, GeoDataCoordinates::Degree ) );
mapWidget->zoomView( 2200 );
mapWidget->show();
} else {
qDebug() << "Unable to open " << inputFile.absoluteFilePath();
}
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 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: