Search code examples
javascriptpythongoogle-mapspyqt4

Plotting Lines of Bearing on a Google Map within a PyQT app


I am adding a new bit of functionality to an existing pyQT based application, the ability to plot lines of bearing on a map.

I have added a QWebView to my app and successfully loaded google maps in it. So far so good.

But if I were now wanting to overlay lines of bearing on to that map, would I have to utilise the google maps javascript API?

In practice I think this means I would have to generate an html page dynamically within the python code, include javascript elements to plot the polylines, then load that html page in the QWebView?

Does that sound about right, or is there a simpler means to achieve the same end?

I'm open to alternative ways of being able to overlay lines on a map, the simpler the better. But it has to be achievable from within a pyQT app.

I've done a similar thing using google maps but from within an android app. In that instance its simpler as you can call the API functions directly (from Java) without having the javascript middle-man..


Solution

  • After a good week of suffering I can shine some light on the subject

    Yes it is possible, but with caveats.

    If you want to do it then I recommend you go down the route of using the most recent version of everything, i.e. Python3 latest and PyQT5 latest, ideally > 5.6 so you get webengine rather than webkit.

    If you have a dated setup then expect some of the javascript stuff not to work. For example, my setup:

    pyqt                      4.11.4                   py27_1  
    python                    2.7.11                        0  
    qt                        4.8.7                         1  
    

    no good! can't even plot a simple polyline

    conda can be your friend in this respect as you can quickly set up sandboxed python environments with different package versions. I tried this setup and I was able to plot a simple polyline:

    pyqt                      5.6.0            py27h4b1e83c_5    anaconda
    python                    2.7.13                        0    anaconda
    qt                        5.6.2                         5    anaconda
    

    note the switch from PyQT4 to PyQT5 but still with Python2.7. I couldn't face porting all my code from 2 to 3, but I may be able to stretch to a QT4 to QT5 port.

    Note I did also try the Python3.5 and PyQT4 combo:

    ('Qt version:', '4.8.7')
    ('SIP version:', '4.16.9')
    ('PyQt version:', '4.11.4')
    

    And this also worked.

    Finally, the one you should probably aim for, Python3 with PyQT5:

    Qt version: 5.5.1
    SIP version: 4.17
    PyQt version: 5.5.1
    

    This also worked.

    I wouldn't recommend using PyQT4 if you are interested in doing anything javascript related with a QWebView widget.

    Once you have a Python and QT environment hooked up to a recent webkit, or even better, webengine build then I would concur that it is relatively easy to get python and javascript working together with a QWebView widget. For my use case of displaying and manipulating googlemaps it is a very powerful approach.

    The reason I say webengine is better than webkit is because QT have ditched webkit in favour of webengine as the browser engine in their most recent version releases, so using webengine will be more future-proof