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.出现的问题

 有时候能够正常显示地图,再不改变任何的情况下有时候有不能显示出地图

Qt5.1.1实现调用google地图显示-LMLPHP

05-28 02:29