ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

智能车载系统#百度地图

智能车载系统#百度地图 地图显示点击搜索按钮即可出现地图初始位置是自定义的因为没有GPS模块。同时可以拖拽页面移动地图位置放大和缩小地图。参考博客https://blog.csdn.net/mbs520/article/details/112873809?spm1001.2014.3001.5502一、ui界面部分二、代码实现部分功能1map.h#ifndef MAP_H #define MAP_H #include QMainWindow #include QWidget #include QNetworkAccessManager #include QNetworkReply #include QDebug #include QFileDialog #include QMessageBox #include QFile #include QPixmap #include QLabel #include mypushbutton.h QT_BEGIN_NAMESPACE namespace Ui { class Map; } QT_END_NAMESPACE class Map : public QWidget { Q_OBJECT public: Map(QWidget *parent nullptr); ~Map(); void sendResquest(QString city,QString markers,QString zoom11); //zoom 为当前地图尺寸编号1zoom18,即放大缩小有次数限制 void Mybutton_init(); double east,north; //东经北纬 public slots: void ready_Read_slots(); private: Ui::Map *ui; bool eventFilter(QObject *watched, QEvent *event); QLabel *label_image; //图片文件 QFile file; QString file_nameNULL; //字符串 QString city,markers,zoom; QString resq_msg; //定时器 QTimer *timer,*timer1; //按钮 myPushButton* zoom_down,*zoom_up,*exit_button,*start_button; //网络 QNetworkRequest resquest; QNetworkAccessManager *manager; QNetworkReply *reply; signals: void map_show_main(); }; #endif // MAP_H2mypushbutton.h#ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H #includeQPushButton #includeQPropertyAnimation #includeQString #includeQEvent #includeQMouseEvent #include QObject #include QWidget class myPushButton : public QPushButton { Q_OBJECT public: myPushButton(QString normal_path,QString press_path,int pixwidth10,int pixheight10); void zoom1(); void zoom2(); private: QString normal_path; QString press_path; QPropertyAnimation* animation; protected: void mousePressEvent(QMouseEvent * e); void mouseReleaseEvent(QMouseEvent * e); signals: public slots: }; #endif // MYPUSHBUTTON_H3map.cpp#include map.h #include ui_map.h #include QByteArray #include QTimer /* * 作者小鑫 */ //移动 int m_x,m_y; double move_x,move_y; Map::Map(QWidget *parent) : QWidget(parent) ,ui(new Ui::Map) { ui-setupUi(this); ui-mapWidget-setStyleSheet(:/pic/map_menu.jpeg); //设置触摸事件 setAttribute(Qt::WA_AcceptTouchEvents); //安装事件过滤器 this-installEventFilter(this); //按钮初始化 Mybutton_init(); connect(start_button,myPushButton::clicked,[]() { start_button-zoom1();//弹跳 //发送请求 city ; east 113.25;//广州 north 23.13; markers QString(%1,%2).arg(east).arg(north); zoom 11; sendResquest(city,markers,zoom);//发送请求 }); connect(zoom_down,myPushButton::clicked,[]() { zoom_down-zoom1();//弹跳 if(zoom.toInt()3) { int num zoom.toInt()-1; zoom.clear(); zoom QString(%1).arg(num); qDebug() resq_msg; sendResquest(city,markers,zoom);//重新发送请求删除现图片打开变化后的图片 } else { zoom QString(3); } // zoom_down-zoom2(); }); connect(zoom_up,myPushButton::clicked,[](){ zoom_up-zoom1();//弹跳 if(zoom.toInt()19) { qDebug() resq_msg; int num zoom.toInt()1; zoom.clear(); zoom QString(%1).arg(num); sendResquest(city,markers,zoom);//发送请求 } else { zoom QString(19); } // zoom_up-zoom2(); }); //定时器初始化 timer1 new QTimer(this); //返回退出按钮下处理 connect(exit_button,myPushButton::clicked,[](){ exit_button-zoom1();//弹跳 //打开文件 system(rm p.info); QString file_n p.info; QFile f_file; f_file.setFileName(file_n); f_file.open(QIODevice::Append | QIODevice::Truncate|QIODevice::ReadWrite); QString point_msg QString(%1\n%2).arg(east).arg(north); f_file.write(point_msg.toUtf8().data()); //p.info写入经纬度 double array f_file.readAll().toDouble(); qDebug()arrayendl; f_file.close(); this-close(); emit map_show_main(); }); } Map::~Map() { delete ui; } //发送请求 void Map::sendResquest(QString city,QString markers,QString zoom) { managernew QNetworkAccessManager(this); manager-disconnect(); disconnect(reply, QIODevice::readyRead,this,Map::ready_Read_slots); resq_msg QString(http://api.map.baidu.com/staticimage?center%1markers%2zoom%3width800height480) .arg(city).arg(markers).arg(zoom); resquest.setUrl(QUrl(resq_msg.toUtf8().data())); replymanager-get(resquest); //发送请求 //重新绑定链接当有数据来时触发信号 // connect(reply, QIODevice::readyRead,this,Map::ready_Read_slots); connect(manager, QNetworkAccessManager::finished, this, Map::ready_Read_slots); } //void Map::Thread01() //{ // qDebug() Thread01 func QThread::currentThreadId(); //// timer1-start(100); //// connect(timer1,QTimer::timeout,[]() //// { //// timer1-stop(); // file.write(reply-readAll()); // file.close(); // QPixmap pix; // if(pix.load(file_name)) // ui-mapWidget-setStyleSheet(border-image: url(./1.png);); // this-show(); //// }); //} //读取槽函数 void Map::ready_Read_slots() { //打开图片文件 // system(rm 1.png); // 用于删除现在可能存在的1.png file_name ./2.png; file.setFileName(file_name); file.open(QIODevice::Append | QIODevice::Truncate); qDebug() ready_Read_slots func QThread::currentThreadId(); file.write(reply-readAll()); file.close(); // QPixmap pix; // if(pix.load(file_name)) ui-mapWidget-setStyleSheet(border-image: url(./2.png);); this-show(); // Thread01(); // timer1-start(100); // connect(timer1,QTimer::timeout,[]() // { // timer1-stop(); // file.write(reply-readAll()); // file.close(); // QPixmap pix; // if(pix.load(file_name)) // ui-mapWidget-setStyleSheet(border-image: url(./1.png);); // this-show(); // }); } //按钮初始化 void Map::Mybutton_init() { //缩小按钮 zoom_downnew myPushButton(:/pic/zoom_down.png,,80,80); zoom_down-setParent(this); zoom_down-move(720,350); //放大按钮 zoom_upnew myPushButton(:/pic/zoom_up.png,,80,80); zoom_up-setParent(this); zoom_up-move(720,250); //返回退出按钮 exit_buttonnew myPushButton(:/pic/exit.png,,60,60); exit_button-setParent(this); exit_button-move(730,20); //开始退出按钮 start_buttonnew myPushButton(:/pic/search.png,,60,60); start_button-setParent(this); start_button-move(730,100); } //事件过滤器 QPoint point,last_point; //按下坐标 bool pres_flag,rele_flag; //按下松开标志 bool Map::eventFilter(QObject *watched, QEvent *event) { if(watched this ){ QTouchEvent* touch static_castQTouchEvent*(event); switch (event-type()) { case QEvent::MouseButtonPress://鼠标按下事件 pres_flag 1; last_point.setY(cursor().pos().y()); // 记录最初按下点的y坐标 last_point.setX(cursor().pos().x()); // 记录最初按下点的x坐标 break; case QEvent::MouseButtonRelease://鼠标松开事件 rele_flag 1; point.setY(cursor().pos().y()); // 记录最后松开点的y坐标 point.setX(cursor().pos().x()); // 记录最后松开点的x坐标 break; case QEvent::TouchBegin://触摸事件 point.setY(touch-touchPoints().at(0).pos().y()); // 记录点的y坐标 point.setX(touch-touchPoints().at(0).pos().x()); // 记录点的x坐标 if(point.x()580 || point.y()350) { rele_flag 1; pres_flag 1; } event-accept(); break; default: break; } } if(pres_flag1 rele_flag1) { pres_flag 0; rele_flag 0; if(event-type() QEvent::TouchBegin)//触摸 { m_x 400-point.x(); m_y 240-point.y(); } else { //鼠标 m_x point.x()-last_point.x(); m_y point.y()-last_point.y(); } if(m_x10 || m_x-10 || m_y10 || m_y-10) { QString m_zoom zoom; //计算需要偏移多少东经北纬 move_x m_x*0.0000003*(19-m_zoom.toInt())*(19-m_zoom.toInt())*(19-m_zoom.toInt())*(19-m_zoom.toInt()); move_y m_y*0.0000003*(19-m_zoom.toInt())*(19-m_zoom.toInt())*(19-m_zoom.toInt())*(19-m_zoom.toInt()); east - move_x; north move_y; markers QString(%1,%2).arg(east).arg(north); sendResquest(city,markers,zoom); //发送图片请求 } } return QWidget::eventFilter(watched,event);//将事件传递给父类 }4mypushbutton.cpp#include mypushbutton.h #include QDebug myPushButton::myPushButton(QString normal_path,QString press_path,int pixwidth,int pixheight) { this-normal_pathnormal_path; this-press_pathpress_path; QPixmap pix; bool ret pix.load(this-normal_path); if(!ret) { qDebug()图片加载失败; return ; } //设置图片固定大小 this-setFixedSize(pixwidth,pixheight); //设置不规则图片样式 this-setStyleSheet( QPushButton{border:0px;} ); //设置图标 this-setIcon(pix); //设置图标大小 this-setIconSize(QSize(pixwidth,pixheight)); animation new QPropertyAnimation(this,geometry); } void myPushButton::zoom1() { //设置动画时间间隔 animation-setDuration(100); //设置起始位置 animation-setStartValue(QRect(this-x(),this-y()10,this-width(),this-height())); //设置结束位置 animation-setEndValue(QRect(this-x(),this-y(),this-width(),this-height())); //设置弹跳曲线 animation-setEasingCurve(QEasingCurve::OutBounce); //执行动画 animation-start(); } void myPushButton::zoom2() { //设置动画时间间隔 animation-setDuration(200); //设置起始位置 animation-setStartValue(QRect(this-x(),this-y(),this-width(),this-height())); //设置结束位置 animation-setEndValue(QRect(this-x(),this-y()-10,this-width(),this-height())); //设置弹跳曲线 animation-setEasingCurve(QEasingCurve::InElastic); //执行动画 animation-start(); } void myPushButton::mousePressEvent(QMouseEvent *e)//鼠标按下 { if(this-press_path ! ) { QPixmap pix; bool ret pix.load(this-press_path); if(!ret) { qDebug()图片加载失败; return ; } //设置图片固定大小 this-setFixedSize(pix.width(),pix.height()); //设置不规则图片样式 this-setStyleSheet(QPushButton{border:0px;}); //设置图标 this-setIcon(pix); //设置图标大小 this-setIconSize(QSize(pix.width(),pix.height())); } return QPushButton::mousePressEvent(e); } void myPushButton::mouseReleaseEvent(QMouseEvent *e)//鼠标释放 { if(this-press_path ! ) { QPixmap pix; bool ret pix.load(this-normal_path); if(!ret) { qDebug()图片加载失败; return ; } //设置图片固定大小 this-setFixedSize(pix.width(),pix.height()); //设置不规则图片样式 this-setStyleSheet(QPushButton{border:0px;}); //设置图标 this-setIcon(pix); //设置图标大小 this-setIconSize(QSize(pix.width(),pix.height())); } return QPushButton::mouseReleaseEvent(e); }三、最终界面
返回列表