• 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. Codeforces Round #451 & Codeforces Round #452

    Rounding Solution Proper Nutrition 枚举 Solution Phone Numbers 模拟 Solution Alarm Clock 贪心,好像不用线段树也可以,事 ...

  2. javascript 公历与农历相互转换工具类

    /** * 公历[1900-1-31,2100-12-31]时间区间内的公历.农历互转 * @charset UTF-8 * @Author Jea杨(JJonline@JJonline.Cn) * ...

  3. 使用WebGL + Three.js制作动画场景

    使用WebGL + Three.js制作动画场景 3D图像,技术,打造产品,还有互联网:这些只是我爱好的一小部分. 现在,感谢WebGL的出现-一个新的JavaScriptAPI,它可以在不依赖任何插 ...

  4. Python 之 基础知识(四)

    一.公共方法(列表.元组.字典以及字符串) 1.内置函数 cmp函数取消可以用比较运算符来代替,但是字典是无序的,故而不可以用比较运算符比较. 2.切片(列表.元组.字符串适用) 3.运算符 列表中直 ...

  5. jquey中的事件绑定

    三种方法: $(selector).live(events, data, handler);                // jQuery 1.3+ $(document).delegate(se ...

  6. 【Oracle】查询当前SCN

    介绍两种方式: 一.sys用户下: select current_scn from v$database; select dbms_flashback.get_system_change_number ...

  7. SVN冲突出现原因及解决方法浅谈

    缘由 很简单,用svn合base,出现了各种各样奇怪的问题,虽然最终没有造成什么大的线上问题,但过程也是曲折的,耗费个人精力,也占用他人资源,不好不好,一点都不佛系. 究其原因,还是对为什么出现各种冲 ...

  8. 文字左右滚动选择,改变direction的值

    1.从左到右滚动显示<marquee direction=left>测试</marquee> 2.从右到左滚动显示<marquee direction=right> ...

  9. Java中成员变量和局部变量区别

    在类中的位置不同 重点 成员变量:类中,方法外 局部变量:方法中或者方法声明上(形式参数) 作用范围不一样 重点 成员变量:类中 局部变量:方法中 初始化值的不同 重点 成员变量:有默认值 局部变量: ...

  10. Java中数组的反转

    public class ArrayDemo2 { public static void main(String[] args) { //定义一个数组存放元素 int[] arr3 = {10, 20 ...