• 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. Tomcat 日志切割

    一.installing 日志轮训工具 yum  install cronolog -y 二.安装.修改tomcat文件 wget  http://mirrors.shuosc.org/apache/ ...

  2. Java基础3一基础语句

    1.条件语句:所谓的条件语句就是指有选择的去执行部分代码. 包括:if条件语句和switch条件语句 if条件语句: 语法: (1)if(条件语句){ //条件成立时需要执行的代码   } (2)if ...

  3. 2014 Container技术大会:未来Linux Container会是PaaS平台的核心

    不应错过2014 Container技术大会的九大理由. 一.Docker官方人员再次来到北京,首次向中国布道Docker技术.2013年Docker高级软件工程师Jerome Petazzoni,曾 ...

  4. Session和Cookie对比详解

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  5. 六星经典CSAPP笔记系列 - 作者:西代零零发

    六星经典CSAPP笔记(1)计算机系统巡游 六星经典CSAPP笔记(2)信息的操作和表示 六星经典CSAPP-笔记(3)程序的机器级表示

  6. RXSwift源码浅析(一)

    简述 最近老大给了个新项目,我打算用Swift写.原来OC用的RAC,换到Swift自然框架也想试试新的,就用了RXSwift,对于这两个框架,我都是会用,但不解其中的原理,正好最近需求没下来,就研究 ...

  7. forEach 列出数组的每个元素:

    数组.forEach便利所有的元素 array.forEach(function(currentValue, index, arr), thisValue) function(currentValue ...

  8. JDK源码中的英文注释翻译(Class)

    public final class Class<T> implements java.io.Serializable, GenericDeclaration, Type, Annotat ...

  9. Django01 web http 基础

    一.内容回顾 1.python基础 2.网络编程 3.并发编程 4.前端 5.数据库(MySQL) 二.今日概要 1.了解Web应用程序的本质 2.Django简介及安装使用 三.今日详细 1.最简单 ...

  10. 莫烦大大TensorFlow学习笔记(8)----优化器

    一.TensorFlow中的优化器 tf.train.GradientDescentOptimizer:梯度下降算法 tf.train.AdadeltaOptimizer tf.train.Adagr ...