PIE-SDK For C++地图鼠标事件监听

1.功能简介

地图鼠标事件包含鼠标的按下MouseButtonPress(),弹起MouseButtonRelease(),移动MouseMove()等事件,通过这些事件可以对地图进行动态的操作,接下来以地图状态栏的信息为例具体介绍如何使用这三种事件。

2.功能实现说明

2.1. 实现思路及原理说明

第一步:在主程序头文件中进行事件捕捉

protected:

    /**

    * @brief 重写eventFilter事件

    * @param[in] QObject *obj 触发事件的对象

    * @param[in] QEvent *ev 触发的事件

    * @return

    */

    bool eventFilter(QObject *obj, QEvent *ev) override;

 

第二步:在.cpp文件中进行实现代码编写

2.2. 核心接口与方法

接口/

方法/属性

说明

SysDisplay::DisplayTransformationPtr

ToMapPoint

屏幕坐标转换为地图坐标

SysGeometry::SpatialReferencePtr

Name

设置或获取空间参考Name

2.3. 示例代码

项目路径

百度云盘地址下/PIE示例程序/02.地图操作/06.地图鼠标事件监听

数据路径

百度云盘地址下/PIE示例数据/栅格数据/04.World/World.tif

视频路径

百度云盘地址下/PIE视频教程/02.地图操作/06.地图鼠标事件监听.avi

示例代码

  1. 头文件中添加事件捕捉

    protected:

     

        /**

        * @brief 重写eventFilter事件

        * @param[in] QObject *obj 触发事件的对象

        * @param[in] QEvent *ev 触发的事件

        * @return

        */

        bool eventFilter(QObject *obj, QEvent *ev) override;

     

  2. 事件代码编写

    //本次示例主要以地图的状态栏

    bool PIEMainWindow::eventFilter(QObject *obj, QEvent *ev)

    {

        if (obj == m_pMapControl)

        {

            if (ev->type() == QEvent::MouseMove)

            {

                QMouseEvent *pMouseEvent = static_cast<QMouseEvent*>(ev);

                m_pLineEdit_ScreenCoordsInfos->setText(QString("%1,%2").arg(pMouseEvent->x()).arg(pMouseEvent->y()));

                double mapX, mapY;

                mapX = mapY = 0;

                m_pMapControl->GetActiveView()->GetDisplayTransformation()->ToMapPoint(pMouseEvent->x(), pMouseEvent->y(), mapX, mapY);

                m_pLineEdit_GeoCoordsInfos->setText(QString("%1,%2").arg(mapX).arg(mapY));

     

                SysGeometry::SpatialReferencePtr ptrSR = m_pMapControl->GetMap()->GetSpatialReference();

                if (ptrSR == nullptr) return true;

                m_pLabel_SpatialReferenceInfos->setText(ptrSR->GetName());

            }

            else if (ev->type()==QEvent::MouseButtonPress)

            {    

                QMouseEvent *pMouseEvent = static_cast<QMouseEvent*>(ev);

                QPoint qPoint;

                qPoint.setX(pMouseEvent->x());

                qPoint.setY(pMouseEvent->y());

                SysGeometry::PointPtr point = new SysGeometry::Point();

                point = m_pMapControl->GetActiveView()->GetDisplayTransformation()->ToMapPoint(qPoint);

                QString screenPoint = QString("屏幕坐标:%1,%2").arg(pMouseEvent->x()).arg(pMouseEvent->y());

                QString mapPoint= QString("屏幕坐标:%1,%2").arg(point->GetX()).arg(point->GetY());

            }

            else if (ev->type()==QEvent::MouseButtonRelease)

            {

                qDebug("鼠标释放事件");

            }

        }

        else if (obj == m_pPageLayoutControl)

        {

            if (ev->type() == QEvent::MouseMove)

            {

                QMouseEvent *pMouseEvent = static_cast<QMouseEvent*>(ev);

     

                m_pLineEdit_ScreenCoordsInfos->setText(QString("%1,%2").arg(pMouseEvent->x()).arg(pMouseEvent->y()));

                double mapX, mapY;

                mapX = mapY = 0;

                m_pPageLayoutControl->GetActiveView()->GetDisplayTransformation()->ToMapPoint(pMouseEvent->x(), pMouseEvent->y(), mapX, mapY);

                m_pLineEdit_GeoCoordsInfos->setText(QString("%1,%2").arg(mapX).arg(mapY));

     

                SysGeometry::SpatialReferencePtr ptrSR = m_pPageLayoutControl->GetMap()->GetSpatialReference();

                if (ptrSR == nullptr) return true;

                m_pLabel_SpatialReferenceInfos->setText(ptrSR->GetName());

            }

        }

        return QMainWindow::eventFilter(obj, ev);

    }

2.4. 示例截图

 

posted @ 2020-02-26 14:39  PIESAT  阅读(367)  评论(0编辑  收藏  举报