How to use Geolocation API with Qtwebkit?

Asked

Viewed 111 times

2

It is possible to use Geolocation API with desktop applications using QWebView?

I tried to use PermissionGrantedByUser, but it doesn’t work:

void WebPage::permissionRequested(QWebFrame* frame, Feature feature)
{
    switch (feature) {
        case Geolocation:
            qDebug() << "GEO-Location: PermissionGrantedByUser";
            setFeaturePermission(frame, feature, PermissionGrantedByUser);
        break;
        default:
            qDebug() << "";
    }
}
  • I tried to QT += positioning, but it doesn’t work
  • I tried to TARGET.CAPABILITY += NetworkServices Location, but it doesn’t work. I believe this flag whether for smartphones, such as Symbian.

The events displayLocation and watchPosition are never fired and used { timeout: ... } will show Timeout expired after the specified time:

<script>
function displayError(err) {
    document.getElementById("geo").innerHTML = ("Código de erro: " + err.code +
        " / mensagem: " + err.message);
}

function displayLocation(position) {
    document.getElementById("geo").innerHTML = [
        "latitude:" + position.coords.latitude,
        "longitude:" + position.coords.longitude
    ].join(", ");
}

function getMyLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(function(a, b, c) {
            console.log(a, b, c);
            document.getElementById("geo").innerHTML = "Testando...";
        });
        navigator.geolocation.getCurrentPosition(displayLocation, displayError, {
            enableHighAccuracy: false,
            timeout: 1000,
            maximumAge: 0
        });
    } else {
        document.getElementById("geo").innerHTML = "Geo-Localização não suportado";
    }
}

window.onload = function() {
    getMyLocation();
};
</script>
<div id="geo"></div>

I believe I have to create a plugin with Qt and put in the folder qtDir/compiler/plugins/position, but I don’t know where to start creating this plugin.

How can I do this?

You can customize events and send simulated coordinates or by a third-party library (I’m not asking for a library or Javascript alternatives) to the navigator.geolocation.getCurrentPosition?


Extra information

Use QT_DEBUG_PLUGINS=1 in the purification mode (debug mode) the Application Output returns this (note that the first line of log is generated by WebPage::permissionRequested):

GEO-Location: PermissionGrantedByUser
QFactoryLoader::QFactoryLoader() checking directory path "C:/Qt5.4.0/5.4/mingw491_32/plugins/position" ...
QFactoryLoader::QFactoryLoader() looking at "C:/Qt5.4.0/5.4/mingw491_32/plugins/position/qtposition_positionpoll.dll"
Found metadata in lib C:/Qt5.4.0/5.4/mingw491_32/plugins/position/qtposition_positionpoll.dll, metadata=
{
    "IID": "org.qt-project.qt.position.sourcefactory/5.0",
    "MetaData": {
        "Keys": [
            "positionpoll"
        ],
        "Monitor": true,
        "Position": false,
        "Priority": 1000,
        "Provider": "positionpoll",
        "Satellite": false
    },
    "className": "QGeoPositionInfoSourceFactoryPoll",
    "debug": false,
    "version": 328704
}


"The plugin 'C:/Qt5.4.0/5.4/mingw491_32/plugins/position/qtposition_positionpoll.dll' uses incompatible Qt library. (Cannot mix debug and release libraries.)"
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "C:/Qt5.4.0/5.4/mingw491_32/plugins/position/qtposition_positionpolld.dll"
Found metadata in lib C:/Qt5.4.0/5.4/mingw491_32/plugins/position/qtposition_positionpolld.dll, metadata=
{
    "IID": "org.qt-project.qt.position.sourcefactory/5.0",
    "MetaData": {
        "Keys": [
            "positionpoll"
        ],
        "Monitor": true,
        "Position": false,
        "Priority": 1000,
        "Provider": "positionpoll",
        "Satellite": false
    },
    "className": "QGeoPositionInfoSourceFactoryPoll",
    "debug": true,
    "version": 328704
}


Got keys from plugin meta data ("positionpoll")
QFactoryLoader::QFactoryLoader() checking directory path "C:/projects/webview-example/debug/position" ...

In other words, when I run Javascript navigator.geolocation the Qt calls the C:/Qt5.4.0/5.4/mingw491_32/plugins/position/qtposition_positionpoll.dll, but does not return any information back to the QWebView.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.