c++ QWebEngineView应用无法加载Google Maps API,js:Uncaught(in promise)错误:无法加载Google Maps JavaScript API

dgiusagp  于 2024-01-09  发布在  Go
关注(0)|答案(1)|浏览(409)

我有一个在QWebEngineView中显示GoogleMap的应用程序。该应用程序在Windows上运行良好,但到目前为止,我还没有成功地让应用程序在Ubuntu或MacOS上运行。为了缩小问题,我修改了Qt Map Example以复制错误。这个测试应用程序是在Qt版本6.4.1上编译的。GCC++编译器是版本11.04。

maps.pro:

  1. TEMPLATE = app
  2. QT += webenginewidgets widgets
  3. HEADERS += \
  4. mainwindow.h
  5. SOURCES += main.cpp \
  6. mainwindow.cpp
  7. CONFIG+=use_gold_linker
  8. target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/maps
  9. INSTALLS += target

字符串

main.cpp

  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
  3. #include "mainwindow.h"
  4. #include <QApplication>
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication::setOrganizationName("QtExamples");
  8. QApplication app(argc, argv);
  9. MainWindow mainWindow;
  10. mainWindow.resize(1024, 768);
  11. mainWindow.show();
  12. return app.exec();
  13. }

mainwindow.h

  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
  3. #ifndef MAINWINDOW_H
  4. #define MAINWINDOW_H
  5. #include <QMainWindow>
  6. #include <QWebEngineView>
  7. class MainWindow : public QMainWindow
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MainWindow(QWidget *parent = nullptr);
  12. private:
  13. QWebEngineView *m_view;
  14. };
  15. #endif // MAINWINDOW_H

主窗口.cpp

  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
  3. #include "mainwindow.h"
  4. #include <QMessageBox>
  5. #include <QDir>
  6. MainWindow::MainWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. , m_view(new QWebEngineView(this))
  9. {
  10. setCentralWidget(m_view);
  11. QWebEnginePage *page = m_view->page();
  12. QFileInfo info(QDir::currentPath() + QDir::separator() + "test.htm");
  13. if(info.exists())
  14. page->load(QUrl::fromLocalFile(info.filePath()));
  15. //page->load(QUrl("https://googlemaps.com"));
  16. }

apikey.js //包含有效的apikey

  1. var apikey2 = "APIKEY"; // API key 3
  2. console.log("api keys loaded");

test.htm //主窗口.cpp加载的html文件:

  1. <html>
  2. <head>
  3. <meta name="viewport" content="initial-scale=1.0" user-scalable="yes" />
  4. <style type="text/css">
  5. html { height: 100%; }
  6. body { height: 100%; margin: 0; padding: 0 }
  7. #map { height: 100%; }
  8. .menu {
  9. width: 160px;
  10. box-shadow: 3px 3px 5px #888888;
  11. border-style: solid;
  12. border-width: 1px;
  13. border-color: grey;
  14. border-radius: 2px;
  15. background-color: #ffff;
  16. position: fixed;
  17. text-align: center;
  18. display: none;
  19. }
  20. .menu-item {
  21. height: 20px;
  22. background-color: white;
  23. }
  24. </style>
  25. <title>Simple Map</title>
  26. <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  27. <!-- playground-hide -->
  28. <script src="apikey.js"> </script>
  29. <script>
  30. const process = { env: {} };
  31. process.env.GOOGLE_MAPS_API_KEY =
  32. apikey2;
  33. </script>
  34. <!-- playground-hide-end -->
  35. <!--link rel="stylesheet" type="text/css" href="./style.css" /-->
  36. <script type="module" src="./test_index.js"></script>
  37. </head>
  38. <body>
  39. <div id="map"></div>
  40. <!-- prettier-ignore -->
  41. <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
  42. ({key: apikey2, v: "weekly"});</script>
  43. </body>
  44. </html>

test_index.js //JavaScript模块

  1. let map; //google.maps.Map;
  2. async function initMap() {
  3. const { Map } = await google.maps.importLibrary("maps");
  4. map = new Map(document.getElementById("map"), {
  5. center: { lat: -34.397, lng: 150.644 },
  6. zoom: 8,
  7. });
  8. console.log("initMap done");
  9. }
  10. initMap();
  11. //export {};


运行程序会导致空白屏幕和以下错误:

  1. 19:15:35: Debugging /home/allen/Projects/build-maps-Desktop_Qt_6_4_1_GCC_64bit-Debug/maps ...
  2. qt.webenginecontext:
  3. GL Type: desktop
  4. Surface Type: OpenGL
  5. Surface Profile: CompatibilityProfile
  6. Surface Version: 4.3
  7. QSG RHI Backend: OpenGL
  8. Using Supported QSG Backend: yes
  9. Using Software Dynamic GL: no
  10. Using Multithreaded OpenGL: yes
  11. Init Parameters:
  12. * application-name maps
  13. * browser-subprocess-path /home/allen/Qt/6.4.1/gcc_64/libexec/QtWebEngineProcess
  14. * create-default-gl-context
  15. * disable-features ConsolidatedMovementXY,InstalledApp,BackgroundFetch,WebOTP,WebPayments,WebUSB,PictureInPicture
  16. * disable-setuid-sandbox
  17. * disable-speech-api
  18. * enable-features NetworkServiceInProcess,TracingServiceInProcess
  19. * enable-threaded-compositing
  20. * in-process-gpu
  21. * use-gl desktop
  22. js: Uncaught (in promise) Error: The Google Maps JavaScript API could not load.
  23. 23:38:02: Debugging of /home/allen/Projects/build-maps-Desktop_Qt_6_4_1_GCC_64bit-Debug/maps has finished with exit code 0.

ssgvzors

ssgvzors1#

问题原来是QWebEngineView不会从本地文件加载页面。我添加了一个qrc资源文件,其中包含test.htm和test_index.js,然后更改了行

  1. page->load(QUrl::fromLocalFile(info.filePath()));

字符串

  1. page->load("qrc:/test.htm");


纠正了这个问题。

相关问题