1.简介
QgsMapTool地图工具是用于操作地图画布的用户交互式工具。例如,地图平移和缩放功能被实现为地图工具。
QgsMapTool是抽象基类,以下是类的继承关系:
2.常用接口
virtual void canvasDoubleClickEvent (QgsMapMouseEvent *e) | 重写鼠标双击事件 |
virtual void canvasMoveEvent (QgsMapMouseEvent *e) | 重写鼠标移动事件 |
virtual void canvasPressEvent (QgsMapMouseEvent *e) | 重写鼠标按下事件 |
virtual void canvasReleaseEvent (QgsMapMouseEvent *e) | 重写鼠标抬起事件 |
virtual void setCursor (const QCursor &cursor) | 设置光标 |
QgsPointXY toMapCoordinates (QPoint point) | 将点从屏幕坐标转换为地图坐标。 |
3.示例
示例1:画布移动、放大、缩小
源码:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_GisCtrl.h"
#include <QDebug>
#include <QFileInfo>
#include <qgsmaptoolpan.h>
#include <qgsrasterlayer.h>
#include <qgsmapcanvas.h>
#include <qgspoint.h>
#include <QMouseEvent>
#include <qgsmaptoolzoom.h>
#include <QActionGroup>
class GisCtrl : public QMainWindow
{
Q_OBJECT
public:
GisCtrl(QWidget *parent = Q_NULLPTR);
private:
Ui::GisCtrlClass ui;
private slots:
void slotXyCoordinates(const QgsPointXY &p);
void slotMove();
void slotZoomIn();
void slotZoomOut();
private:
QgsMapCanvas *m_mapCanvas = nullptr;
QList<QgsMapLayer *> m_layers;
QgsMapToolPan *m_toolPan = nullptr; // 新建移动工具
QgsMapToolZoom *m_toolZoomIn = nullptr;//放大
QgsMapToolZoom *m_toolZoomOut = nullptr;//缩小
QActionGroup *m_actionGroup = nullptr;
};
#include "GisCtrl.h"
#include <qgsrasterlayer.h>
#include <qgsrubberband.h>
#pragma execution_character_set("utf-8")
GisCtrl::GisCtrl(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//创建画布
m_mapCanvas = new QgsMapCanvas(this);
QString url = "F:/arcgis/TMS.xml";
m_mapCanvas->setCanvasColor(QColor(255, 255, 255));
//加载瓦片地图
QgsRasterLayer *layer = new QgsRasterLayer(url);
if (!layer->isValid())
{
qDebug() << "load map faild";
}
m_layers.append(layer);
// 新建移动工具
m_toolPan = new QgsMapToolPan(m_mapCanvas);
m_mapCanvas->setExtent(layer->extent());
m_mapCanvas->enableAntiAliasing(true);
m_mapCanvas->setLayers(m_layers);
m_mapCanvas->setMapTool(m_toolPan);
m_toolZoomIn = new QgsMapToolZoom(m_mapCanvas, false);
m_toolZoomOut = new QgsMapToolZoom(m_mapCanvas, true);
m_actionGroup = new QActionGroup(this);
m_actionGroup->addAction(ui.actionMove);
m_actionGroup->addAction(ui.actionZoomIn);
m_actionGroup->addAction(ui.actionZoomOut);
m_actionGroup->setExclusive(true);
connect(m_mapCanvas, &QgsMapCanvas::xyCoordinates, this, &GisCtrl::slotXyCoordinates);
connect(ui.actionMove, &QAction::triggered, this, &GisCtrl::slotMove);
connect(ui.actionZoomIn, &QAction::triggered, this, &GisCtrl::slotZoomIn);
connect(ui.actionZoomOut, &QAction::triggered, this, &GisCtrl::slotZoomOut);
setCentralWidget(m_mapCanvas);
ui.actionMove->setChecked(true);
ui.actionLineManager->setVisible(false);
ui.actionLayer->setVisible(false);
ui.actionSelect->setVisible(false);
ui.actionItem->setVisible(false);
}
void GisCtrl::slotXyCoordinates(const QgsPointXY &p)
{
QString str = QString("经度= %1,纬度=%2").arg(QString::number(p.x(), 'f', 6)).arg(QString::number(p.y(), 'f', 6));
this->statusBar()->showMessage(str);
}
void GisCtrl::slotMove()
{
m_mapCanvas->setMapTool(m_toolPan);
}
void GisCtrl::slotZoomIn()
{
m_mapCanvas->setMapTool(m_toolZoomIn);
}
void GisCtrl::slotZoomOut()
{
m_mapCanvas->setMapTool(m_toolZoomOut);
}
示例2:鼠标点击画矩形
源码:
自定义一个地图交互工具继承QgsMapToolIdentify
#pragma once
#include <QObject>
#include <QList>
#include <QScopedPointer>
#include <qgsmapcanvas.h>
#include <qgsmaptoolidentify.h>
#include <qgsrubberband.h>
#include <qgsmapmouseevent.h>
class QgsMapToolSelectFeatures : public QgsMapToolIdentify
{
Q_OBJECT
public:
QgsMapToolSelectFeatures(QgsMapCanvas *mapCanvas);
protected:
//重写鼠标移动
void canvasMoveEvent(QgsMapMouseEvent *e) override;
//重写鼠标按下
void canvasPressEvent(QgsMapMouseEvent *e) override;
void initRubberBand();
private:
// 是否正在选择
bool mSelectionActive = false;
QScopedPointer<QgsRubberBand> mSelectionRubberBand;
QColor mFillColor = QColor(255, 255, 0, 63);
QColor mStrokeColor = QColor(255, 255, 0, 100);
QPoint mInitDragPos;
};
//cpp
#include "QgsMapToolSelectFeatures.h"
#include "QgsMapToolIdentify.h"
QgsMapToolSelectFeatures::QgsMapToolSelectFeatures(QgsMapCanvas *mapCanvas)
: QgsMapToolIdentify(mapCanvas)
{
}
void QgsMapToolSelectFeatures::canvasMoveEvent(QgsMapMouseEvent * e)
{
if (e->buttons() != Qt::LeftButton)
return;
QRect rect;
if (!mSelectionActive)
{
mSelectionActive = true;
rect = QRect(e->pos(), e->pos());
}
else
{
rect = QRect(e->pos(), mInitDragPos);
}
if (mSelectionRubberBand)
mSelectionRubberBand->setToCanvasRectangle(rect);
}
void QgsMapToolSelectFeatures::canvasPressEvent(QgsMapMouseEvent * e)
{
if (!mSelectionRubberBand)
initRubberBand();
mInitDragPos = e->pos();
}
void QgsMapToolSelectFeatures::initRubberBand()
{
mSelectionRubberBand.reset(new QgsRubberBand(mCanvas, QgsWkbTypes::PolygonGeometry));
mSelectionRubberBand->setFillColor(mFillColor);
mSelectionRubberBand->setStrokeColor(mStrokeColor);
}
使用
QgsMapToolSelectFeatures *m_tool = new QgsMapToolSelectFeatures(m_mapCanvas);
m_mapCanvas->setMapTool(m_tool);