Qt5.1.1实现调用google地图显示
2024-08-26 13:45:41
1.调用google地图工程文件
QT +=webkit
QT +=webkitwidgets
QT += core gui greaterThan(QT_MAJOR_VERSION, ): QT += widgets TARGET = googlemap
TEMPLATE = app SOURCES += main.cpp\
mainwindow.cpp HEADERS += mainwindow.h
FORMS += mainwindow.ui RESOURCES += \
google.qrc
2.mainwindow头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include<QMainWindow>
#include<QtWebKit>
#include<QtWebKitWidgets/QtWebKitWidgets>
#include<QtWebKitWidgets/QWebView>
#include<QFile>
#include<QMessageBox>
#include<QTextStream>
#include<QtWebKitWidgets/QWebFrame> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); private slots: void on_webView_loadFinished(bool arg1); private: Ui::MainWindow *ui;
QString buf;
QString s_longitude;
QString s_latitude; }; #endif // MAINWINDOW_H
3 mianwindow源文件
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); s_longitude = "23.2706716"; s_latitude = "113.2931106"; QFile file("E:/QtCreator/Qt5.1.1/Qtlianxi/googlemap/google.html"); if (! file.open(QIODevice::ReadOnly)) {//进行判断 QMessageBox::information(NULL, QObject::tr("Unable to open file"), file.errorString()); } ui->webView->settings()->setAttribute(QWebSettings::PluginsEnabled, true); ui->webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, true); ui->webView->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); ui->webView->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true); ui->webView->settings()->setAttribute(QWebSettings::JavaEnabled, true); QTextStream out(&file); QString output_file = out.readAll(); ui->webView->setHtml(output_file);//显示网页内容 } MainWindow::~MainWindow() {delete ui;} void MainWindow::on_webView_loadFinished(bool arg1) { QWebFrame *qframe = ui->webView->page()->mainFrame(); float longitude = s_longitude.toFloat(); float latitude = s_latitude.toFloat(); buf = QString("Open(%1, %2, %3);").arg(longitude).arg(latitude).arg(QString("\"center\"")); qframe->evaluateJavaScript(buf); ui->label->setText(s_longitude); ui->label_2->setText(s_latitude);
}
4.mianwindow的ui文件
5.html文件
<!DOCTYPE html> <html> <head> <title>Google Maps JavaScript API v3 Example: Map Simple</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta charset="UTF-8"> <style type="text/css"> html, body, #map_canvas { margin: ; padding: ; height: %; } </style> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map; function initialize() { var myOptions = { zoom: , center: new google.maps.LatLng(23.271016, 113.295088), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); Open(x,y,"center"); } function Open (x,y, text) { var myLatlng = new google.maps.LatLng(x,y); map.setCenter (myLatlng, ); var marker = new google.maps.Marker({ position: myLatlng, title:text }); marker.setMap(map); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map_canvas"></div> </body> </html>
6.出现的问题
有时候能够正常显示地图,再不改变任何的情况下有时候有不能显示出地图
最新文章
- TweenMax参数说明
- linux 终端全局代理设置
- Serializable 序列化使用限制
- Linux 操作系统位数(32or64)识别
- sql查询表说明
- MySQL---连接器(python如何操作数据库媒介,基于python语言)
- jQuery插件ImgAreaSelect 实例讲解二
- CRM客户关系管理系统(五)
- ASP.NET Zero--Migration控制台应用程序
- SAI窗口无法移动
- 借网站日记分析~普及一下Pandas基础
- PYMySQL的注册功能的实现
- MAC终端密钥登录自动输入密码
- AX_Function
- Fetch的使用; Yarn命令集; NVM的管理;VueCLi3的使用;
- Java基础复习
- JavaScript -- Document-open
- VS新建一个模板工程
- ISP图像处理&;&;相机系统
- Idempotent --------幂等
热门文章
- SPOJ375 Query on a tree(树链剖分)
- PYday15--面向对象的进阶:集成、成员、方法、异常处理
- IE6 下绝对定位position:absolute 与浮动不显示 (IE6 下拉菜单显示)
- python redis中blpop和lpop的区别
- API经济时代的思考(转载目的:为之后写API-first模式的生命周期治理做准备)
- layui.code代码装饰器
- hibernate与struts框架实现增删改查
- 使用runtime关联对象将视图添加到视图的类目里
- 学习orm框架及一些看法
- javascript中 for循环的一些写法 for length 以及for in 还有 for of 的区别