• widget.h

     #ifndef MAPWIDGET_H
    #define MAPWIDGET_H #include <QGraphicsView>
    #include <QLabel>
    #include <QMouseEvent> class MapWidget : public QGraphicsView
    {
    Q_OBJECT
    public:
    MapWidget(); void readMap(); //读取地图信息
    QPointF mapToMap(QPointF); //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值 public slots:
    void slotZoom(int); protected:
    void drawBackground(QPainter *painter, const QRectF &rect); //完成地图显示的功能
    void mouseMoveEvent(QMouseEvent *event); //鼠标移动功能 private:
    QPixmap map; //地图
    qreal zoom; //大小
    QLabel *viewCoord; //标签
    QLabel *sceneCoord;
    QLabel *mapCoord;
    double x1,y1;
    double x2,y2;
    }; #endif // MAPWIDGET_H
  • widget.cpp
     #include "widget.h"
    #include <QSlider>
    #include <QGridLayout>
    #include <QFile>
    #include <QTextStream>
    #include <QGraphicsScene>
    #include <math.h> MapWidget::MapWidget()
    {
    readMap(); //读取地图信息 zoom=; int width = map.width();
    int height = map.height();
    QGraphicsScene *scene = new QGraphicsScene(this);
    scene->setSceneRect(-width/,-height/,width,height);
    setScene(scene);
    setCacheMode(CacheBackground); //用于地图缩放的滑动条
    QSlider *slider = new QSlider;
    slider->setOrientation(Qt::Vertical);
    slider->setRange(,);
    slider->setTickInterval();
    slider->setValue();
    connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int))); QLabel *zoominLabel = new QLabel;
    zoominLabel->setScaledContents(true);
    zoominLabel->setPixmap(QPixmap("zoomin.png")); QLabel *zoomoutLabel = new QLabel;
    zoomoutLabel->setScaledContents(true);
    zoomoutLabel->setPixmap(QPixmap("zoomout.png")); //坐标值显示区
    QLabel *label1 = new QLabel(tr("GraphicsView:"));
    viewCoord = new QLabel;
    QLabel *label2 = new QLabel(tr("GraphicsScene:"));
    sceneCoord = new QLabel;
    QLabel *label3 = new QLabel(tr("map:"));
    mapCoord = new QLabel; //坐标显示区布局
    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(label1,,);
    gridLayout->addWidget(viewCoord,,);
    gridLayout->addWidget(label2,,);
    gridLayout->addWidget(sceneCoord,,);
    gridLayout->addWidget(label3,,);
    gridLayout->addWidget(mapCoord,,);
    gridLayout->setSizeConstraint(QLayout::SetFixedSize); QFrame *coordFrame = new QFrame;
    coordFrame->setLayout(gridLayout); //缩放控制子布局
    QVBoxLayout *zoomLayout = new QVBoxLayout;
    zoomLayout->addWidget(zoominLabel);
    zoomLayout->addWidget(slider);
    zoomLayout->addWidget(zoomoutLabel); //坐标显示区域布局
    QVBoxLayout *coordLayout = new QVBoxLayout;
    coordLayout->addWidget(coordFrame);
    coordLayout->addStretch(); //主布局
    QHBoxLayout *mainLayout = new QHBoxLayout;
    //滑动
    mainLayout->addLayout(zoomLayout);
    //坐标
    mainLayout->addLayout(coordLayout);
    mainLayout->addStretch();
    mainLayout->setMargin();
    mainLayout->setSpacing();
    setLayout(mainLayout); setWindowTitle("Map Widget");
    setMinimumSize(,);
    } void MapWidget::readMap() //读取地图信息
    {
    QString mapName;
    QFile mapFile("maps.txt");
    //打开文件
    int ok = mapFile.open(QIODevice::ReadOnly);
    if(ok)
    {
    QTextStream ts(&mapFile);
    if(!ts.atEnd())
    {
    ts>>mapName;
    ts>>x1>>y1>>x2>>y2;
    }
    }
    //载入地图
    map.load(mapName);
    } void MapWidget::slotZoom(int value) //地图缩放与放大
    {
    qreal s;
    if(value>zoom) //放大
    {
    s=pow(1.01,(value-zoom));
    }
    else //缩小
    {
    s=pow(/1.01,(zoom-value));
    }
    scale(s,s);//放大缩小
    // rotate(s);
    zoom = value;
    } //绘制背景
    void MapWidget::drawBackground(QPainter *painter, const QRectF &rect)
    {
    painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()), map);//绘图
    } void MapWidget::mouseMoveEvent(QMouseEvent *event)
    {
    //QGraphicsView 坐标
    QPoint viewPoint = event->pos();
    viewCoord->setText(QString::number(viewPoint.x())+","+QString::number(viewPoint.y())); //QGraphicsScene 坐标
    QPointF scenePoint = mapToScene(viewPoint);
    sceneCoord->setText(QString::number(scenePoint.x())+","+QString::number(scenePoint.y())); //地图坐标(经、纬度值)
    QPointF latLon = mapToMap(scenePoint);
    mapCoord->setText(QString::number(latLon.x())+","+QString::number(latLon.y()));
    } //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值
    QPointF MapWidget::mapToMap(QPointF p)
    {
    QPointF latLon;
    qreal w =sceneRect().width();
    qreal h =sceneRect().height(); qreal lon = y1-((h/+p.y())*abs(y1-y2)/h);
    qreal lat = x1+((w/+p.x())*abs(x1-x2)/w); latLon.setX(lat);
    latLon.setY(lon); return latLon;
    }
  • main.c
     #include "widget.h"
    #include <QApplication> int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    //设置字体
    QFont font("ARPL KaitiM GB",);
    font.setBold(true);
    a.setFont(font); //显示
    MapWidget w;
    w.show(); return a.exec();
    }

36.QT地图的更多相关文章

  1. Foundations of Qt Development 学习笔记 Part1 Tips1-50

    1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...

  2. html代码转义到js时,往往会遇到问题,这代码实现html和js互转

    这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  3. Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)

    摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...

  4. Qt5.7学习

    一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...

  5. Qt 学习之路 2(36):二进制文件读写

    Qt 学习之路 2(36):二进制文件读写 豆子 2013年1月6日 Qt 学习之路 2 20条评论 在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice ...

  6. Qt开发北斗定位系统融合百度地图API及Qt程序打包发布

    Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...

  7. Qt的QWebChannel和JS、HTML通信/交互驱动百度地图

    Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...

  8. Qt加载百度离线地图

    1.下载百度地图离线API 1.3 下载链接:http://download.csdn.NET/detail/caoshangpa/9476608,网上虽然出现了2.0版本离线API,但是经试用,存在 ...

  9. Qt编写安防视频监控系统17-在线地图

    一.前言 在线地图模块在一开始设计整个系统的时候就考虑进去了,主要功能就是在摄像机管理中,提供经纬度信息,然后加载百度地图在浏览器中显示,根据摄像机信息表中的每个摄像机的经纬度信息,自动生成设备点在地 ...

随机推荐

  1. 【MFC】基于opencv的趣味相机

    为了参加学校的科技节,故用mfc随手制作了一个名为<趣味相机>的小程序: 其中对图形图像处理运用到了opencv. 效果图 这界面逼格低了点╭(╯^╰)╮ 有兴趣的朋友可以在此下载尝试:h ...

  2. layoutInflater的用途以及获取VIEW方法

    如果需要用到自定义多个布局,就需要用到layoutInflater,获取layoutInflater一般有几种方式,但我在实际使用中,感觉如下的getLayoutInflater()是最为方便的,不用 ...

  3. Android PopupWindow使用方法小结

    前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多 ...

  4. java 简单工厂模式实现

    简单工厂模式:也可以叫做静态工厂方法,属于类创建型模式,根据不同的参数,返回不同的类实现. 主要包含了三个角色: A.抽象产品角色 一般用接口 或是 抽象类实现 B.具体的产品角色,具体的类的实现 C ...

  5. VHDL之package

    Pacakge Frequently used pieces of VHDL code are usually written in the form of COMPONENTS, FUNCTIONS ...

  6. 国外60个专业3D模型网站

    原始链接:http://blog.sina.com.cn/s/blog_4ba3c7950100jxkh.html Today, 3D models are used in a wide variet ...

  7. Win7系统下调整硬盘分区大小给C盘更多的空间

    电脑安装了很多程序,C盘空间越来越小了.如何给C盘调整更多的空间,其实只要调整硬盘分区大小便可解决这个问题,下面有个小技巧,需要的朋友照做就可以了 Win7系统下如何调整硬盘分区大小,以前装系统的时候 ...

  8. jquery选择器的一些处理

    本文不讨论用jquery选择器具体怎么选择页面元素,而讨论选择元素后后的一些处理 jquery的选择器选择元素的时候,即使没有选择到指定的对象,页面并不会报错,例子: <!doctype htm ...

  9. input type=”file“ change事件只执行一次的问题

    js解决办法 HTML:<input id="file",type="file" onchange="upload()" /> ...

  10. 【BZOJ3065】带插入区间k小值

    题意: 带插入,修改的区间k小值在线查询 原序列长度<=35000,插入个数<=35000,修改个数<=70000,0<=权值<=70000 题解: Orz vfleak ...