36.QT地图
- 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地图的更多相关文章
- Foundations of Qt Development 学习笔记 Part1 Tips1-50
1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...
- html代码转义到js时,往往会遇到问题,这代码实现html和js互转
这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)
摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...
- Qt5.7学习
一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...
- Qt 学习之路 2(36):二进制文件读写
Qt 学习之路 2(36):二进制文件读写 豆子 2013年1月6日 Qt 学习之路 2 20条评论 在上一章中,我们介绍了有关QFile和QFileInfo两个类的使用.我们提到,QIODevice ...
- Qt开发北斗定位系统融合百度地图API及Qt程序打包发布
Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...
- Qt的QWebChannel和JS、HTML通信/交互驱动百度地图
Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...
- Qt加载百度离线地图
1.下载百度地图离线API 1.3 下载链接:http://download.csdn.NET/detail/caoshangpa/9476608,网上虽然出现了2.0版本离线API,但是经试用,存在 ...
- Qt编写安防视频监控系统17-在线地图
一.前言 在线地图模块在一开始设计整个系统的时候就考虑进去了,主要功能就是在摄像机管理中,提供经纬度信息,然后加载百度地图在浏览器中显示,根据摄像机信息表中的每个摄像机的经纬度信息,自动生成设备点在地 ...
随机推荐
- js-字符串方法
字符串 遍历字符串 方法:(类似数组) 使用for 或 for… in 结果:得到字符串中的每个字符 查找字符 ² charAt(索引值) 注: 超出索引值范围时,则返回空字符 ² ch ...
- jQuery中事件模块介绍
事件模块 1.提供其他DOM方法 包括:next 和 nextAll方法 1.1 next方法实现 目标:扩展框架方法,获取当前元素的下一个元素 问题:如何获取下一个元素? 1.1.1 提供 next ...
- knockout.js(js)代码在IE中出现“意外地调用了方法或属性”的错误
var CartListViewModel = function () { var self = this; self.payment = [ { name: "", value: ...
- MySQL快速创造百万测试数据
CREATE TABLE `vote_record_memory` ( `id` INT (11) NOT NULL AUTO_INCREMENT, `user_id` VARCHAR (20) NO ...
- Oracle中的SAVEPOINT
学习存储过程中使用断点回滚事务时,发现目前网络上存在一个问题,那就是使用断点回滚后,都忘记了一个很重要的事情,提交事务.虽然使用了断点回滚,但是断点回滚不像rollBack或commit一样结束当前事 ...
- 「JavaSE 重新出发」05.03.03 使用反射编写泛型数组代码
Employee[] a = new Employee[100]; // ... // array is full a = Arrays.copyOf(a, 2 * a.length); 如何编写这样 ...
- 移动前端头部标签(HTML5 head meta)转载
移动web页面头部书写 字数2516 阅读1128 评论0 喜欢30 HTTP 标题信息(http-equiv) 和页面描述信息(name) http-equiv:该枚举的属性定义,可以改变服务器和用 ...
- bzoj 3730: 震波 动态点分治_树链剖分_线段树
##### 题目描述 : 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i].不幸的是,这片土地常常发生地震,并且随着 ...
- SQLServer Oracle MySQL的区别
table tr:nth-child(odd){ background: #FFFFCC; font-size: 18px; } table tr:nth-child(even){ backgroun ...
- Bind for 0.0.0.0:80 failed: port is already allocated.解决方案
一句话总结就是容器占用的port还没有完全释放 查看进程,发现相关的容器并没有在运行,而 docker-proxy 却依然绑定着端口: $ docker ps 检查docker镜像 $ ps -aux ...