Marble/Runners/Search

From KDE TechBase
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.