mapviewer.cpp

This application is just a simple map viewer. A Mapadapter is created (OpenStreetmaps) and added to a layer. The layer is given to the MapControl. Two Buttons are available to adjust the zoom level. If the window is resized the map widget will adjust its size.

You can find this example here: QMapControl/Samples/Mapviewer

sample_mapviewer.png

screenshot

#include "mapviewer.h"
Mapviewer::Mapviewer(QWidget *parent)
    : QMainWindow(parent)
{
    // create MapControl
    mc = new MapControl(QSize(380, 540));
    mc->showScale(true);

    // create mapadapter, for mainlayer and overlay
    mapadapter = new EmptyMapAdapter();

    // create a layer with the mapadapter and type MapLayer
    mainlayer = new MapLayer("OpenStreetMap-Layer", mapadapter);

    // add Layer to the MapControl
    mc->addLayer(mainlayer);

    addZoomButtons();

    // show mapcontrol in mainwindow
    setCentralWidget(mc);

    FixedImageOverlay* fip = new FixedImageOverlay(-102, 77, 0,0, QCoreApplication::applicationDirPath() + "/bild.png");

    mainlayer->addGeometry(fip);

    connect(mc, SIGNAL(mouseEventCoordinate(const QMouseEvent*, const QPointF)),
                          this, SLOT(coordinateClicked(const QMouseEvent*, const QPointF)));

}
void Mapviewer::addZoomButtons()
{
    // create buttons as controls for zoom
    QPushButton* zoomin = new QPushButton("+");
    QPushButton* zoomout = new QPushButton("-");
    zoomin->setMaximumWidth(50);
    zoomout->setMaximumWidth(50);

    connect(zoomin, SIGNAL(clicked(bool)),
            mc, SLOT(zoomIn()));
    connect(zoomout, SIGNAL(clicked(bool)),
            mc, SLOT(zoomOut()));

    // add zoom buttons to the layout of the MapControl
    QVBoxLayout* innerlayout = new QVBoxLayout;
    innerlayout->addWidget(zoomin);
    innerlayout->addWidget(zoomout);
    mc->setLayout(innerlayout);
}


Mapviewer::~Mapviewer()
{
}

// resize the widget
void Mapviewer::resizeEvent ( QResizeEvent * event )
{
    mc->resize(event->size());
}

void Mapviewer::coordinateClicked(const QMouseEvent * evnt, const QPointF coordinate)
{
    if (evnt->type()==QEvent::MouseButtonPress)
    {
        qDebug() << coordinate << ": " << evnt->x() << " / " << evnt->y();
    }
}

Generated on Wed Jul 29 12:38:09 2009 for QMapControl by  doxygen 1.5.9