// Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \example webenginewidgets/maps \examplecategory {Web Technologies} \title WebEngine Widgets Maps Example \ingroup webengine-widgetexamples \brief Demonstrates how to handle geolocation requests. \image maps-example.png \e {Maps} demonstrates how to handle geolocation requests originating from a \l QWebEnginePage. The \l {https://www.w3.org/TR/geolocation-API/}{Geolocation API} is a JavaScript API that web applications can use to determine the user's physical location to show on a map, for example. As \QWE relies on the \e {Qt Positioning} module to power this API, a viable location backend is needed for the target platform. To avoid accidentally sending location information to third parties geolocation requests are denied by default. This example demonstrates the steps an application must take in order to start accepting these requests. \note On Windows 11, enable settings to grant the application access to Windows location services. In the Settings App under \uicontrol {Privacy & Security} > \uicontrol {Location}, enable \uicontrol {Location services}, \uicontrol {Let apps access your location} and \uicontrol {Let desktop apps access your location}. \include examples-run.qdocinc \section1 The Code The example program consists of a single class, \c MainWindow, inheriting from \l QMainWindow: \quotefromfile webenginewidgets/maps/mainwindow.h \skipto #include \printuntil /^\}/ In the constructor we first set up the \l QWebEngineView as the central widget: \quotefromfile webenginewidgets/maps/mainwindow.cpp \skipto MainWindow::MainWindow \printuntil setCentralWidget We then proceed to connect a lambda function to the \l QWebEnginePage::permissionRequested signal: \skipto m_view->page() \printuntil QWebEnginePermission permission This signal is emitted whenever a web page requests to make use of a certain feature or device, including not only location services but also audio capture devices or mouse locking, for example. In this example we only handle requests for location services: \printuntil return Now comes the part where we actually ask the user for permission: \printuntil }); Note that the question includes the host component of the web site's URI (\c permission.origin()) to inform the user as to exactly which web site will be receiving their location data. We use the \l QWebEnginePermission::grant() and \l QWebEnginePermission::deny() methods to communicate the user's answer back to the web page. Finally we ask the \l QWebEnginePage to load the web page that might want to use location services: \printuntil /^\}/ \sa {html5-geolocation}{Qt WebEngine HTML5 Geolocation} */