Skip to content

Instantly share code, notes, and snippets.

@nilsnolde
Forked from ataffanel/map.html
Created October 11, 2017 09:13
Show Gist options
  • Save nilsnolde/4dd879a93ae18837aa95f17e1fc4836a to your computer and use it in GitHub Desktop.
Save nilsnolde/4dd879a93ae18837aa95f17e1fc4836a 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_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment