js: Uncaught ReferenceError: google is not defined false



  • Hi,all
    I want to integrate Google Maps APIs with Qt 5.7 . I used QWebengineview to load Google Map url and so it works fine . However , when using webview->page()->runjavascript() to manipulate javascript function , I finally got error js: Uncaught ReferenceError: google is not defined false . I cant figure out how to resolve this problem. any idea?
    See my myproject here.

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
           QUrl url("qrc:/map.html");
    
           QWebEngineSettings::globalSettings()->setAttribute(QWebEngineSettings::PluginsEnabled,true);
           QWebEngineSettings::globalSettings()->setAttribute(QWebEngineSettings::JavascriptEnabled,true);
           QWebEnginePage *page = new QWebEnginePage(this);
           QWebChannel *channel = new QWebChannel(this);
           page->load(url);
           page->setWebChannel(channel);
           ui->widget->setPage(page);
    
            QString js=QString("var marker = new google.maps.Marker({ ")+
            QString("position: new google.maps.LatLng(24.705311, -74.2581939),")+
            QString(" map: map,")+
            QString("title: \"Marker A\",")+
            QString("   }); ");
    
           ui->widget->page()->runJavaScript(js,[](const QVariant &result){ qDebug() << result.toBool(); });
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    map.html

    <!DOCTYPE html>
    <html>
      <head>
        <style>
           #map {
            height: 400px;
            width: 100%;
           }
        </style>
      </head>
      <body>
        <h3>My Google Maps Demo</h3>
        <div id="map"></div>
        <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA_iIaYqH8KQkC4rVm2IRLAqHiBawE0_Ag&callback=initMap">
        </script>
        <script>
          function initMap() {
            var uluru = {lat: -25.363, lng: 131.044};
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 4,
              center: uluru
            });
            var marker = new google.maps.Marker({
              position: uluru,
              map: map
            });
          }
        </script>
      </body>
    </html>
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.