Marble/Runners/Search: Difference between revisions

From KDE TechBase
m (fix tutorial header)
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/MarbleCPlusPlus}}
 
{{TutorialBrowser|
{{TutorialBrowser|


Line 6: Line 6:
name=Search|
name=Search|


pre=[[Projects/Marble/Routing/BasicRouting|Tutorial 6 - Basic Routing]]|
pre=[[Projects/Marble/Routing/BasicRouting|Tutorial 9 - Basic Routing]]|


next=[[Projects/Marble/Runners/ReverseGeocoding|Tutorial 8 - Reverse Geocoding]]|  
next=[[Projects/Marble/Runners/ReverseGeocoding|Tutorial 11 - Reverse Geocoding]]|  
}}
}}


Line 20: Line 20:
#include <marble/MarbleWidget.h>
#include <marble/MarbleWidget.h>
#include <marble/MarbleModel.h>
#include <marble/MarbleModel.h>
#include <marble/MarbleRunnerManager.h>
#include <marble/SearchRunnerManager.h>
#include <marble/GeoDataPlacemark.h>
#include <marble/GeoDataPlacemark.h>


Line 28: Line 28:
{
{
     QApplication app(argc,argv);
     QApplication app(argc,argv);
     MarbleWidget *mapWidget = new MarbleWidget;
     MarbleModel *model = new MarbleModel;
    MarbleModel *model = mapWidget->model();
      
      
     MarbleRunnerManager* manager = new MarbleRunnerManager( model->pluginManager() );
     SearchRunnerManager* manager = new SearchRunnerManager( model );
    manager->setModel( model );
      
      
     QVector<GeoDataPlacemark*> searchResult = manager->searchPlacemarks( "Karlsruhe" );
     QVector<GeoDataPlacemark*> searchResult = manager->searchPlacemarks( "Karlsruhe" );
Line 61: Line 59:
</source>
</source>


The latest source code of this example can be found [https://quickgit.kde.org/?p=marble.git&a=blob&f=examples%2Fcpp%2Fsearch%2Fmain.cpp here].
{{tip|
{{tip|
Here's a little checklist to tackle some problems that might arise when compiling the code above:
Here's a little checklist to tackle some problems that might arise when compiling the code above:


* You need Qt and '''Marble development packages''' (or comparable git installations), version 1.3 (Marble library 0.13), shipped e.g. with KDE 4.8
* You need Qt and '''Marble development packages''' (or comparable git installations), version 1.3 (Marble library 0.13), shipped post KDE 4.8
* If ''Qt headers'' are not installed in '''/usr/include/qt4''' on your system, change the path in the g++ call above accordingly.
* If ''Qt headers'' are not installed in '''/usr/include/qt4''' on your system, change the path in the g++ call above accordingly.
* Likewise, '''add -I /path/to/marble/headers''' if they're not to be found in /usr/include
* Likewise, '''add -I /path/to/marble/headers''' if they're not to be found in /usr/include

Latest revision as of 09:29, 21 October 2016

Search
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 9 - Basic Routing
What's Next   Tutorial 11 - Reverse Geocoding
Further Reading   n/a

Searching for cities, addresses, points of interest

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 search for an arbitrary string (Karlsruhe in the example below, see Userbase for more information on search terms).

#include <QtGui/QApplication>
#include <QtCore/QDebug>

#include <marble/MarbleWidget.h>
#include <marble/MarbleModel.h>
#include <marble/SearchRunnerManager.h>
#include <marble/GeoDataPlacemark.h>

using namespace Marble;
 
int main(int argc, char** argv)
{
    QApplication app(argc,argv);
    MarbleModel *model = new MarbleModel;
    
    SearchRunnerManager* manager = new SearchRunnerManager( model );
    
    QVector<GeoDataPlacemark*> searchResult = manager->searchPlacemarks( "Karlsruhe" );
    foreach( GeoDataPlacemark* placemark, searchResult ) {
        qDebug() << "Found " << placemark->name() << "at" << placemark->coordinate().toString();
    }
}

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 and the output looks similar to this:

Found  "Karlsruhe, Germany" at "  8° 33' 48.7"E,  49° 05' 38.4"N" 
Found  "Karlsruhe, McLean" at "100° 36' 58.4"W,  48° 05' 27.7"N" 
Found  "Karlsruhe, Karlsruhe, Stadt" at "  8° 24' 16.0"E,  49° 00' 50.6"N" 
Found  "Karlsruhe, Remscheid" at "  7° 17' 35.3"E,  51° 09' 09.4"N" 
Found  "Karlsruhe, Austria" at " 15° 19' 53.6"E,  47° 21' 33.4"N" 
Found  "Karlsruhe, McLean" at "100° 37' 13.5"W,  48° 05' 24.0"N" 
Found  "Karlsruhe, Sohland a.d. Spree" at " 14° 27' 36.5"E,  51° 02' 28.5"N" 
Found  "Parkstraße, Bad Elster" at " 12° 14' 08.0"E,  50° 16' 57.9"N" 
Found  "Karlsruhe (Bruchsal)" at "  8° 33' 48.7"E,  49° 05' 38.4"N" 
Found  "Karlsruhe (Innenstadt-West)" at "  8° 24' 16.0"E,  49° 00' 50.6"N"

The latest source code of this example can be found here.

Tip
Here's a little checklist to tackle some problems that might arise when compiling the code above:
  • You need Qt and Marble development packages (or comparable git installations), version 1.3 (Marble library 0.13), shipped post KDE 4.8
  • If Qt headers are not installed in /usr/include/qt4 on your system, change the path in the g++ call above accordingly.
  • Likewise, add -I /path/to/marble/headers if they're not to be found in /usr/include
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.