Marble/Runners/LoadingKML: Difference between revisions
CezarMocan (talk | contribs) |
m (Ochurlaud moved page Projects/Marble/Runners/LoadingKML to Marble/Runners/LoadingKML) |
||
(12 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{TutorialBrowser| | {{TutorialBrowser| | ||
Line 7: | Line 6: | ||
name=Search| | name=Search| | ||
pre=[[Projects/Marble/ | pre=[[Projects/Marble/MarbleSignalsSlots|Tutorial 3 - Basic Interactions with MarbleWidget]]| | ||
next=[[Projects/Marble/Runners/ | next=[[Projects/Marble/Runners/LoadingOSM|Tutorial 5 - Loading OSM files into MarbleWidget]]|}} | ||
}} | |||
== 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 | 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 open a .kml (or .gpx, .osm, ...) file and display it into the Marble Widget. | ||
<source lang="cpp-qt"> | <source lang="cpp-qt"> | ||
Line 20: | Line 18: | ||
#include <QtCore/QFileInfo> | #include <QtCore/QFileInfo> | ||
#include <QtGui/QApplication> | #include <QtGui/QApplication> | ||
#include <marble/MarbleWidget.h> | #include <marble/MarbleWidget.h> | ||
#include <marble/MarbleModel.h> | #include <marble/MarbleModel.h> | ||
using namespace Marble; | using namespace Marble; | ||
Line 38: | Line 33: | ||
return 1; | return 1; | ||
} | } | ||
MarbleWidget *mapWidget = new MarbleWidget(); | MarbleWidget *mapWidget = new MarbleWidget(); | ||
mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml"); | 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(); | 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: | ||
Line 67: | Line 53: | ||
</source> | </source> | ||
If things go fine, execute <tt>./my_marble some-file.kml</tt> and you get a Marble Widget which displays your KML file. For example, download [http:// | If things go fine, execute <tt>./my_marble some-file.kml</tt> and you get a Marble Widget which displays your KML file. For example, download and unpack [http://techbase.kde.org/images.techbase/b/bd/Bucharest.kml.gz Bucharest.kml] (a [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1GeoDataLinearRing.html LinearRing] representing Bucharest's city boundaries), place it in the same folder as your <tt>my_marble.cpp</tt> file and run <tt>./my_marble bucharest.kml</tt>. The result should be similar to this: | ||
[[Image:LoadKML.png]] | [[Image:LoadKML.png]] | ||
{{ | |||
note| | |||
The same method works for loading other file types too, like OpenStreetMap (.osm), GPX and Shapefiles (.shp). | |||
}} | |||
Latest revision as of 21:01, 10 March 2016
Tutorial Series | Marble C++ Tutorial |
Previous | Tutorial 3 - Basic Interactions with MarbleWidget |
What's Next | Tutorial 5 - Loading OSM files into MarbleWidget |
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 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>
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;
}
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: