Skip to content

Instantly share code, notes, and snippets.

@ataffanel
Created April 20, 2015 14:12
Show Gist options
  • Save ataffanel/13aac4b79bc0080b881f to your computer and use it in GitHub Desktop.
Save ataffanel/13aac4b79bc0080b881f to your computer and use it in GitHub Desktop.
Simple OSM map view in pyqt using QtWebkit
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
body { padding: 0; margin: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
var map = L.map('map').setView([55.61121, 12.99351], 16);
L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>',
subdomains: '1234',
}).addTo(map);
var marker = L.marker(map.getCenter()).addTo(map);
marker.bindPopup("Hello World!").openPopup();
if(typeof MainWindow != 'undefined') {
var onMapMove = function() { MainWindow.onMapMove(map.getCenter().lat, map.getCenter().lng) };
map.on('move', onMapMove);
onMapMove();
}
#!/usr/bin/env python
from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork
import functools
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi()
self.show()
self.raise_()
def setupUi(self):
#self.setFixedSize(800, 500)
vbox = QtGui.QVBoxLayout()
self.setLayout(vbox)
label = self.label = QtGui.QLabel()
sp = QtGui.QSizePolicy()
sp.setVerticalStretch(0)
label.setSizePolicy(sp)
vbox.addWidget(label)
view = self.view = QtWebKit.QWebView()
cache = QtNetwork.QNetworkDiskCache()
cache.setCacheDirectory("cache")
view.page().networkAccessManager().setCache(cache)
view.page().networkAccessManager()
view.page().mainFrame().addToJavaScriptWindowObject("MainWindow", self)
view.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks)
view.load(QtCore.QUrl('map.html'))
view.loadFinished.connect(self.onLoadFinished)
view.linkClicked.connect(QtGui.QDesktopServices.openUrl)
vbox.addWidget(view)
button = QtGui.QPushButton('Go to Paris')
panToParis = functools.partial(self.panMap, 2.3272, 48.8620)
button.clicked.connect(panToParis)
vbox.addWidget(button)
def onLoadFinished(self):
with open('map.js', 'r') as f:
frame = self.view.page().mainFrame()
frame.evaluateJavaScript(f.read())
@QtCore.pyqtSlot(float, float)
def onMapMove(self, lat, lng):
self.label.setText('Lng: {:.5f}, Lat: {:.5f}'.format(lng, lat))
def panMap(self, lng, lat):
frame = self.view.page().mainFrame()
frame.evaluateJavaScript('map.panTo(L.latLng({}, {}));'.format(lat, lng))
if __name__ == '__main__':
app = QtGui.QApplication([])
w = MainWindow()
app.exec_()
@mapofemergence
Copy link

mapofemergence commented Sep 22, 2018

Thank you for this @ataffanel.
I would love to write some code based on yours, for the UI of a project of mine. Please note that the project is completely non profit.
I would publish my code under BSD license but, since I couldn't find any mention of licensing in here, I would ask if you're ok with the idea.
Be sure that I would give credits and point to your gist in the published code; in case you want to know more about the project first, please feel free to ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment