zoukankan      html  css  js  c++  java
  • 36.QT地图

    • widget.h
       1 #ifndef MAPWIDGET_H
       2 #define MAPWIDGET_H
       3 
       4 #include <QGraphicsView>
       5 #include <QLabel>
       6 #include <QMouseEvent>
       7 
       8 class MapWidget : public QGraphicsView
       9 {
      10     Q_OBJECT
      11 public:
      12     MapWidget();
      13 
      14     void readMap();                         //读取地图信息
      15     QPointF mapToMap(QPointF);              //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值
      16 
      17 public slots:
      18     void slotZoom(int);
      19 
      20 protected:
      21     void drawBackground(QPainter *painter, const QRectF &rect);         //完成地图显示的功能
      22     void mouseMoveEvent(QMouseEvent *event);                            //鼠标移动功能
      23 
      24 private:
      25     QPixmap map;        //地图
      26     qreal zoom;         //大小
      27     QLabel *viewCoord;  //标签
      28     QLabel *sceneCoord;
      29     QLabel *mapCoord;
      30     double x1,y1;
      31     double x2,y2;
      32 };
      33 
      34 #endif // MAPWIDGET_H
    • widget.cpp
        1 #include "widget.h"
        2 #include <QSlider>
        3 #include <QGridLayout>
        4 #include <QFile>
        5 #include <QTextStream>
        6 #include <QGraphicsScene>
        7 #include <math.h>
        8 
        9 MapWidget::MapWidget()
       10 {
       11     readMap();                                    //读取地图信息
       12 
       13     zoom=50;
       14 
       15     int width = map.width();
       16     int height = map.height();
       17     QGraphicsScene *scene = new QGraphicsScene(this);
       18     scene->setSceneRect(-width/2,-height/2,width,height);
       19     setScene(scene);
       20     setCacheMode(CacheBackground);
       21 
       22     //用于地图缩放的滑动条
       23     QSlider *slider = new QSlider;
       24     slider->setOrientation(Qt::Vertical);
       25     slider->setRange(1,100);
       26     slider->setTickInterval(10);
       27     slider->setValue(50);
       28     connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int)));
       29 
       30     QLabel *zoominLabel = new QLabel;
       31     zoominLabel->setScaledContents(true);
       32     zoominLabel->setPixmap(QPixmap("zoomin.png"));
       33 
       34     QLabel *zoomoutLabel = new QLabel;
       35     zoomoutLabel->setScaledContents(true);
       36     zoomoutLabel->setPixmap(QPixmap("zoomout.png"));
       37 
       38     //坐标值显示区
       39     QLabel *label1 = new QLabel(tr("GraphicsView:"));
       40     viewCoord = new QLabel;
       41     QLabel *label2 = new QLabel(tr("GraphicsScene:"));
       42     sceneCoord = new QLabel;
       43     QLabel *label3 = new QLabel(tr("map:"));
       44     mapCoord = new QLabel;
       45 
       46     //坐标显示区布局
       47     QGridLayout *gridLayout = new QGridLayout;
       48     gridLayout->addWidget(label1,0,0);
       49     gridLayout->addWidget(viewCoord,0,1);
       50     gridLayout->addWidget(label2,1,0);
       51     gridLayout->addWidget(sceneCoord,1,1);
       52     gridLayout->addWidget(label3,2,0);
       53     gridLayout->addWidget(mapCoord,2,1);
       54     gridLayout->setSizeConstraint(QLayout::SetFixedSize);
       55 
       56     QFrame *coordFrame = new QFrame;
       57     coordFrame->setLayout(gridLayout);
       58 
       59     //缩放控制子布局
       60     QVBoxLayout *zoomLayout = new QVBoxLayout;
       61     zoomLayout->addWidget(zoominLabel);
       62     zoomLayout->addWidget(slider);
       63     zoomLayout->addWidget(zoomoutLabel);
       64 
       65     //坐标显示区域布局
       66     QVBoxLayout *coordLayout = new QVBoxLayout;
       67     coordLayout->addWidget(coordFrame);
       68     coordLayout->addStretch();
       69 
       70     //主布局
       71     QHBoxLayout *mainLayout = new QHBoxLayout;
       72     //滑动
       73     mainLayout->addLayout(zoomLayout);
       74     //坐标
       75     mainLayout->addLayout(coordLayout);
       76     mainLayout->addStretch();
       77     mainLayout->setMargin(30);
       78     mainLayout->setSpacing(10);
       79     setLayout(mainLayout);
       80 
       81     setWindowTitle("Map Widget");
       82     setMinimumSize(600,400);
       83 }
       84 
       85 void MapWidget::readMap()            //读取地图信息
       86 {
       87     QString mapName;
       88     QFile mapFile("maps.txt");
       89     //打开文件
       90     int ok = mapFile.open(QIODevice::ReadOnly);
       91     if(ok)
       92     {
       93         QTextStream ts(&mapFile);
       94         if(!ts.atEnd())
       95         {
       96             ts>>mapName;
       97             ts>>x1>>y1>>x2>>y2;
       98         }
       99     }
      100     //载入地图
      101     map.load(mapName);
      102 }
      103 
      104 void MapWidget::slotZoom(int value)       //地图缩放与放大
      105 {
      106     qreal s;
      107     if(value>zoom)                         //放大
      108     {
      109         s=pow(1.01,(value-zoom));
      110     }
      111     else                                 //缩小
      112     {
      113         s=pow(1/1.01,(zoom-value));
      114     }
      115     scale(s,s);//放大缩小
      116    // rotate(s);
      117     zoom = value;
      118 }
      119 
      120 //绘制背景
      121 void MapWidget::drawBackground(QPainter *painter, const QRectF &rect)
      122 {
      123     painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()), map);//绘图
      124 }
      125 
      126 void MapWidget::mouseMoveEvent(QMouseEvent *event)
      127 {
      128     //QGraphicsView 坐标
      129     QPoint viewPoint = event->pos();
      130     viewCoord->setText(QString::number(viewPoint.x())+","+QString::number(viewPoint.y()));
      131 
      132     //QGraphicsScene 坐标
      133     QPointF scenePoint = mapToScene(viewPoint);
      134     sceneCoord->setText(QString::number(scenePoint.x())+","+QString::number(scenePoint.y()));
      135 
      136     //地图坐标(经、纬度值)
      137     QPointF latLon = mapToMap(scenePoint);
      138     mapCoord->setText(QString::number(latLon.x())+","+QString::number(latLon.y()));
      139 }
      140 
      141 //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值
      142 QPointF MapWidget::mapToMap(QPointF p)
      143 {
      144     QPointF latLon;
      145     qreal w =sceneRect().width();
      146     qreal h =sceneRect().height();
      147 
      148     qreal lon = y1-((h/2+p.y())*abs(y1-y2)/h);
      149     qreal lat = x1+((w/2+p.x())*abs(x1-x2)/w);
      150 
      151     latLon.setX(lat);
      152     latLon.setY(lon);
      153 
      154     return latLon;
      155 }
    • main.c
       1 #include "widget.h"
       2 #include <QApplication>
       3 
       4 int main(int argc, char *argv[])
       5 {
       6     QApplication a(argc, argv);
       7     //设置字体
       8     QFont font("ARPL KaitiM GB",12);
       9     font.setBold(true);
      10     a.setFont(font);
      11 
      12     //显示
      13     MapWidget w;
      14     w.show();
      15 
      16     return a.exec();
      17 }
  • 相关阅读:
    git线上操作
    IDEA快捷方式
    Java 四种线程池
    java 获取当前天之后或之前7天日期
    如何理解AWS 网络,如何创建一个多层安全网络架构
    申请 Let's Encrypt 通配符 HTTPS 证书
    GCE 部署 ELK 7.1可视化分析 nginx
    使用 bash 脚本把 AWS EC2 数据备份到 S3
    使用 bash 脚本把 GCE 的数据备份到 GCS
    nginx 配置 https 并强制跳转(lnmp一键安装包)
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8796768.html
Copyright © 2011-2022 走看看