代码已经更新:http://www.cnblogs.com/douzujun/p/6272667.html

 //地图显示功能
#ifndef MAPWIDGET_H
#define MAPWIDGET_H #include <QGraphicsView>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#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
//mapwidget.cpp
#include "mapwidget.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对象为主窗口连接一个场景
QGraphicsScene *scene = new QGraphicsScene(this);
//限定场景的显示区域为地图大小
scene->setSceneRect (-width/, -height/, width, height);
setScene (scene);
/***
* The background is cached(隐藏,缓存). This affects both custom backgrounds, and
* backgrounds based on the backgroundBrush property. When this flag is enabled,
* QGraphicsView will allocate one pixmap with the full size of the viewport
*/
setCacheMode (CacheBackground);
/***
* 用于地图缩放的滑动条
* 新建一个QSlider对象作为地图的缩放控制
*/
QSlider *slider = new QSlider;
//设置滚动条方向-垂直
slider->setOrientation (Qt::Vertical);
slider->setRange (, ); //设置地图缩放比例值范围为0~100
slider->setTickInterval (); //显示刻度间隔为10
slider->setValue (); //设置当前初始值为50
// 将缩放控制条的valueChanged()信号与地图缩放slotZoom()槽函数相关联
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 (); //设置对话框(或窗体)的边距为30
mainLayout->setSpacing (); //设定各个控件之间的间距为10
setLayout (mainLayout); //在场景中设置为布局
setWindowTitle ("Map Widget");
setMinimumSize (, );
} //读取地图信息
void MapWidget::readMap ()
{
QString mapName;
//新建一个QFile对象,“map.txt"是描述地图信息的文本文件
QFile mapFile("maps.txt");
//以"只读"的方式打开此文件
int ok = mapFile.open (QIODevice::ReadOnly);
if (ok) //分别以读取地图的名称和四个经纬度信息
{
QTextStream ts(&mapFile);
if (!ts.atEnd ()) { //没有到末尾返回false
ts >> mapName; //储存字符串
ts >> x1 >> y1 >> x2 >> y2; //储存地图左上角和右下角的经纬度
}
}
map.load (mapName); //将地图读取至私有标量map中
} //根据缩放滑动条的当前值,确定缩放的比例,调用scale()函数实现地图缩放--完成地图缩放功能slotZoom
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);
zoom = value; //当前放大值
} //drawBackground()--以地图图片重绘场景的背景来实现地图显示
void MapWidget::drawBackground (QPainter *painter, const QRectF &rect)
{
/***
* The scene rectangle defines the extent of the scene, and in the view's case,
* this means the area of the scene that you can navigate using the scroll bars.
*/
painter->drawPixmap (int(sceneRect ().left ()) + , int(sceneRect ().top ()) - , map); // //Demo 在图片上绘线
// QPen pen;
// pen.setWidth (4);
// pen.setColor (Qt::red);
// painter->setPen (pen);
// painter->setBrush (Qt::red);
// painter->drawLine (100, 10, 100, 100); } //完成某点在各层坐标中的映射及显示
void MapWidget::mouseMoveEvent (QMouseEvent *event)
{
//QGraphicesView 坐标
QPoint viewPoint = event->pos (); //鼠标事件位置
viewCoord->setText (QString::number (viewPoint.x ()) + "," + QString::number (viewPoint.y ())); //QGraphiccsScene 坐标 -- 将视图坐标转换成场景坐标
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 ())); } //完成从场景至地图坐标的转换mapToMap()
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;
}
 //mainwidget
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include "mapwidget.h"
#include <QToolButton>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QTextEdit> class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(QWidget *parent = );
~MainWindow();
void createToolBar();
void paintEvent (QPaintEvent *);
public slots:
void setStartStation();
void setEndStation();
void FindPath();
void Clear();
private:
MapWidget *mapWidget;
QLabel *startLabel;
QLabel *endLabel;
QComboBox *startComboBox;
QComboBox *endComboBox;
QToolButton *findPathBtn;
QToolButton *clearBtn;
}; #endif // MAINWINDOW_H
 //布局实现-mainwindow.cpp

 #include "mainwindow.h"
#include <QToolBar> MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
mapWidget = new MapWidget;
createToolBar (); //实现一个工具栏
setCentralWidget (mapWidget);
setMinimumSize (, ); //设置最小尺寸
} MainWindow::~MainWindow()
{
} void MainWindow::createToolBar ()
{
QToolBar *toolBar = addToolBar ("Tool");
startLabel = new QLabel(tr("起点: "));
startComboBox = new QComboBox;
startComboBox->addItem (tr("公寓6号楼")); startComboBox->addItem (tr("公寓10号楼"));
startComboBox->addItem (tr("公寓5号楼")); startComboBox->addItem (tr("公寓9号楼"));
startComboBox->addItem (tr("公寓4号楼")); startComboBox->addItem (tr("公寓8号楼"));
startComboBox->addItem (tr("公寓3号楼")); startComboBox->addItem (tr("公寓7号楼"));
startComboBox->addItem (tr("公寓2号楼"));
startComboBox->addItem (tr("公寓1号楼")); startComboBox->addItem (tr("图书馆"));
startComboBox->addItem (tr("一食堂")); startComboBox->addItem (tr("西操场"));
startComboBox->addItem (tr("公寓23号楼")); startComboBox->addItem (tr("公寓13号楼"));
startComboBox->addItem (tr("公寓22号楼")); startComboBox->addItem (tr("公寓12号楼")); endLabel = new QLabel(tr("\t终点: ")); endComboBox = new QComboBox;
endComboBox->addItem (tr("公寓6号楼")); endComboBox->addItem (tr("公寓10号楼"));
endComboBox->addItem (tr("公寓5号楼")); endComboBox->addItem (tr("公寓9号楼"));
endComboBox->addItem (tr("公寓4号楼")); endComboBox->addItem (tr("公寓8号楼"));
endComboBox->addItem (tr("公寓3号楼")); endComboBox->addItem (tr("公寓7号楼"));
endComboBox->addItem (tr("公寓2号楼"));
endComboBox->addItem (tr("公寓1号楼")); endComboBox->addItem (tr("图书馆"));
endComboBox->addItem (tr("一食堂")); endComboBox->addItem (tr("西操场"));
endComboBox->addItem (tr("公寓23号楼"));endComboBox->addItem (tr("公寓13号楼"));
endComboBox->addItem (tr("公寓22号楼"));endComboBox->addItem (tr("公寓12号楼")); connect (startComboBox, SIGNAL(activated(int)), this, SLOT(setStartStation()));
connect (endComboBox, SIGNAL(activated(int)), this, SLOT(setEndStation())); findPathBtn = new QToolButton;
findPathBtn->setText (tr("\t\t绘制最短路径")); connect (findPathBtn, SIGNAL(clicked(bool)), this, SLOT(FindPath())); clearBtn = new QToolButton;
clearBtn->setText (tr("\t\t清除")); connect (clearBtn, SIGNAL(clicked(bool)), this, SLOT(Clear())); toolBar->addWidget (startLabel);
toolBar->addWidget (startComboBox);
toolBar->addWidget (endLabel);
toolBar->addWidget (endComboBox);
toolBar->addWidget (findPathBtn);
toolBar->addWidget (clearBtn);
} void MainWindow::setStartStation ()
{ } void MainWindow::setEndStation ()
{ } void MainWindow::FindPath ()
{ } void MainWindow::Clear ()
{ } void MainWindow::paintEvent (QPaintEvent *)
{ }

QT_地图导航的更多相关文章

  1. QT_地图导航 源码下载

    https://github.com/douzujun/MyMapView 主要算法讲解: 1. 计算最短路径(dijkstra算法) Step1: (1)找到最短路径已经确定的顶点,从它已经确定的顶 ...

  2. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块

    config.xml文件的配置如下: <widget left="10" top="50" config="widgets/Navigation ...

  3. iOS开发之百度地图导航

    本篇主要讲述百度地图的导航功能: 第一步:在使用百度导航之前,我们需要在百度地图开放平台上下载导航的 SDK,共85.8M,网速不好的同学可提前准备好. 第二步:引入导航所需的系统包 将AudioTo ...

  4. iOS打开百度地图、高德地图导航

    1.判断手机里是否已经安装了百度地图或者高德地图: BOOL hasBaiduMap = NO; BOOL hasGaodeMap = NO; if ([[UIApplication sharedAp ...

  5. iOS开发----调用地图导航

    注意:本文章下的代码有个别变量未知,所以是不能直接跑通的,我也是转别人的 在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_L ...

  6. iOS 调用地图导航

    在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevic ...

  7. Windows phone 8 学习笔记(8) 定位地图导航(转)

    Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模拟器中测试新地图貌似比较理想.本节主要讲解下位置服务以及新地图控件的 ...

  8. IOS 手绘地图导航

    手绘地图导航 第三方库 NAMapKit, 1)支持在手绘图上标记.缩放 2)支持在单张图片 3)支持瓦片小图片 思路 前提:美工已经切好手绘图,并告知我们当前的缩放级别. 1)确定好手绘图左上角点在 ...

  9. 实现百度地图导航Demo的语音播报功能

    上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...

随机推荐

  1. 一个将 footer 保持在底部的最好方法

    原文: Quick Tip: The Best Way To Make Sticky Footers 当你在布局网页时,有可能会遇到类似下面的这种情况 导致这一问题的原因是页面内容太少,无法将内容区域 ...

  2. entrar en su zapatilla de deporte en este lugar

    Mientras que yo apareció su campo usando nuestro Nike Glide Wildhorse sólo dos ($ 110) zapatillas de ...

  3. Oracle以15分钟为界,统计一天内各时间段的数据笔数

    db.table替换为自己的表名,StartTime为date字段 select count(*), (case floor((to_char(StartTime,'mi'))/15) when 0 ...

  4. Pivot Table

    1. Disable menu 'Disable show/hide Field list menu sht.PivotTables().EnableFieldList = False ''scrip ...

  5. FORM

    一 .新增的input输入属性 1.email类型 在表单提交E-mail地址时,无效的输入会生成很多无效数据,对后期的数据检索造成一定的影响.所以在表单提交之前,需要对输入的E-mail地址进行有效 ...

  6. Could not find or load main class org.gradle.wrapper.GradleWrapperMain解决办法

    解决办法: gradlew is the gradle wrapper executable - batch script on windows and shell script elsewhere. ...

  7. 2.0、Hibernate框架的简单搭建

    一.Hibernate:是一个开放源代码的对象关系映射框架,对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句 ...

  8. ORACLE 常见错误

    ora-00904 :   标识符无效:查询语句中的列或表在oracle 中不存在:

  9. Template function 函数模板用法

    #include<iostream> using namespace std; const double PI = 3.1415926; template <class T> ...

  10. Uva 11732 strcmp() Anyone?

    strcmp() Anyone? Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Subm ...