最近学习pyqt,在网上搜索到了做Google map的,发现那个使用图片作为显示的,感觉那样很不方便,所以想以HTML显示.运用pyqt4的QWebView显示
第一步申请Google map api key,然后进行开发.
第二步,写个运用Google map api进行解析的工具 GoogleMapsUtil.py.代码如下:
import urllib
GMap_KEY_ID = 'YOUR GOOGLE MAP API KEY'
def getGMapsGEO(qaddr):
geo_url = 'http://maps.google.com/maps/geo?'+urllib.urlencode({'q':qaddr})+'&output=csv&key='+GMap_KEY_ID
gdata = urllib.urlopen(geo_url)
gdataArray = gdata.read().split(',')
return gdataArray
def getGMapsStaticUrl(Latitudes, Longitudes, zoom=14, width=512, height=512):
#print "--"+str(Latitudes)+"--"+str(Longitudes)+"--"+str(zoom)
map_url = 'http://maps.google.com/staticmap?center='+str(Latitudes)+','+str(Longitudes)+'&zoom='+str(zoom)+
'&size='+str(width)+'x'+str(height)+'&maptype=mobile&key='+GMap_KEY_ID+'&sensor=false'
return map_url
第三步,用qt designer拉出界面,然后转化为python代码,Ui_GoogleMaps.py 代码如下:
from PyQt4 import QtCore, QtGui
class Ui_DialogGoogleMaps(object):
def setupUi(self, DialogGoogleMaps):
DialogGoogleMaps.setObjectName("DialogGoogleMaps")
DialogGoogleMaps.resize(773, 588)
self.inputAddress = QtGui.QLineEdit(DialogGoogleMaps)
self.inputAddress.setGeometry(QtCore.QRect(40, 30, 371, 20))
self.inputAddress.setObjectName("inputAddress")
self.labelAddress = QtGui.QLabel(DialogGoogleMaps)
self.labelAddress.setGeometry(QtCore.QRect(10, 30, 31, 16))
self.labelAddress.setObjectName("labelAddress")
self.groupBoxMapCtrl = QtGui.QGroupBox(DialogGoogleMaps)
self.groupBoxMapCtrl.setGeometry(QtCore.QRect(530, 40, 211, 121))
self.groupBoxMapCtrl.setObjectName("groupBoxMapCtrl")
self.ButtonUp = QtGui.QPushButton(self.groupBoxMapCtrl)
self.ButtonUp.setGeometry(QtCore.QRect(90, 10, 31, 31))
self.ButtonUp.setObjectName("ButtonUp")
self.ButtonDown = QtGui.QPushButton(self.groupBoxMapCtrl)
self.ButtonDown.setGeometry(QtCore.QRect(90, 90, 31, 31))
self.ButtonDown.setObjectName("ButtonDown")
self.ButtonLeft = QtGui.QPushButton(self.groupBoxMapCtrl)
self.ButtonLeft.setGeometry(QtCore.QRect(30, 50, 31, 31))
self.ButtonLeft.setObjectName("ButtonLeft")
self.ButtonRight = QtGui.QPushButton(self.groupBoxMapCtrl)
self.ButtonRight.setGeometry(QtCore.QRect(150, 50, 31, 31))
self.ButtonRight.setObjectName("ButtonRight")
self.ButtonZoom = QtGui.QPushButton(self.groupBoxMapCtrl)
self.ButtonZoom.setGeometry(QtCore.QRect(70, 50, 31, 31))
self.ButtonZoom.setObjectName("ButtonZoom")
self.ButtonNarrow = QtGui.QPushButton(self.groupBoxMapCtrl)
self.ButtonNarrow.setGeometry(QtCore.QRect(110, 50, 31, 31))
self.ButtonNarrow.setObjectName("ButtonNarrow")
self.MapView = QtWebKit.QWebView(DialogGoogleMaps)
self.MapView.setGeometry(QtCore.QRect(10, 60, 512, 512))
self.MapView.setUrl(QtCore.QUrl("http://maps.google.com/staticmap?center=32.0583650,118.7964680&zoom=14&size=512x512&maptype=mobile&markers=32.0583650,118.7964680&key=ABQIAAAApIB1Ubv-TkAKBJ37W0Hh2RS1AC4DxUbsxJ-9A5H8anlW8PhTrBQW71UJo3SK1Lm1LK_DZxfeCJessA&sensor=false"))
self.MapView.setObjectName("MapView")
self.BtnSearch = QtGui.QPushButton(DialogGoogleMaps)
self.BtnSearch.setGeometry(QtCore.QRect(440, 30, 75, 23))
self.BtnSearch.setObjectName("BtnSearch")
self.ListAddress = QtGui.QListWidget(DialogGoogleMaps)
self.ListAddress.setGeometry(QtCore.QRect(530, 200, 211, 371))
self.ListAddress.setFrameShape(QtGui.QFrame.StyledPanel)
self.ListAddress.setObjectName("ListAddress")
QtGui.QListWidgetItem(self.ListAddress)
QtGui.QListWidgetItem(self.ListAddress)
self.label = QtGui.QLabel(DialogGoogleMaps)
self.label.setGeometry(QtCore.QRect(530, 180, 54, 14))
self.label.setObjectName("label")
self.retranslateUi(DialogGoogleMaps)
QtCore.QMetaObject.connectSlotsByName(DialogGoogleMaps)
def retranslateUi(self, DialogGoogleMaps):
DialogGoogleMaps.setWindowTitle(QtGui.QApplication.translate("DialogGoogleMaps", "Google maps应用程序", None, QtGui.QApplication.UnicodeUTF8))
self.labelAddress.setText(QtGui.QApplication.translate("DialogGoogleMaps", "地址:", None, QtGui.QApplication.UnicodeUTF8))
self.groupBoxMapCtrl.setTitle(QtGui.QApplication.translate("DialogGoogleMaps", "地图控制", None, QtGui.QApplication.UnicodeUTF8))
self.ButtonUp.setText(QtGui.QApplication.translate("DialogGoogleMaps", "上", None, QtGui.QApplication.UnicodeUTF8))
self.ButtonDown.setText(QtGui.QApplication.translate("DialogGoogleMaps", "下", None, QtGui.QApplication.UnicodeUTF8))
self.ButtonLeft.setText(QtGui.QApplication.translate("DialogGoogleMaps", "左", None, QtGui.QApplication.UnicodeUTF8))
self.ButtonRight.setText(QtGui.QApplication.translate("DialogGoogleMaps", "右", None, QtGui.QApplication.UnicodeUTF8))
self.ButtonZoom.setText(QtGui.QApplication.translate("DialogGoogleMaps", "大", None, QtGui.QApplication.UnicodeUTF8))
self.ButtonNarrow.setText(QtGui.QApplication.translate("DialogGoogleMaps", "小", None, QtGui.QApplication.UnicodeUTF8))
self.BtnSearch.setText(QtGui.QApplication.translate("DialogGoogleMaps", "搜索", None, QtGui.QApplication.UnicodeUTF8))
self.ListAddress.setSortingEnabled(False)
__sortingEnabled = self.ListAddress.isSortingEnabled()
self.ListAddress.setSortingEnabled(False)
self.ListAddress.item(0).setText(QtGui.QApplication.translate("DialogGoogleMaps", "结果1", None, QtGui.QApplication.UnicodeUTF8))
self.ListAddress.item(1).setText(QtGui.QApplication.translate("DialogGoogleMaps", "新建项目", None, QtGui.QApplication.UnicodeUTF8))
self.ListAddress.setSortingEnabled(__sortingEnabled)
self.label.setText(QtGui.QApplication.translate("DialogGoogleMaps", "搜索结果:", None, QtGui.QApplication.UnicodeUTF8))
from PyQt4 import QtWebKit
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
DialogGoogleMaps = QtGui.QDialog()
ui = Ui_DialogGoogleMaps()
ui.setupUi(DialogGoogleMaps)
DialogGoogleMaps.show()
sys.exit(app.exec_())
第四步:写界面的动作插槽,GoogleMapsExec.py,代码如下:
# -*- coding: utf-8 -*-
"""
Module implementing GoogleMapsExec.
"""
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature
from Ui_GoogleMaps import Ui_DialogGoogleMaps
import GoogleMapsUtil
class GoogleMapsExec(QDialog, Ui_DialogGoogleMaps):
"""
Class documentation goes here.
"""
jindu = 32.058365
weidu = 118.796468
zoom = 14
def __init__(self, parent = None):
"""
Constructor
"""
QDialog.__init__(self, parent)
self.setupUi(self)
@pyqtSignature("")
def on_ButtonUp_clicked(self):
"""
Slot documentation goes here.
"""
self.jindu =self.jindu-0.01
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
@pyqtSignature("")
def on_ButtonDown_clicked(self):
"""
Slot documentation goes here.
"""
self.jindu = self.jindu+0.01
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
@pyqtSignature("")
def on_ButtonLeft_clicked(self):
"""
Slot documentation goes here.
"""
self.weidu = self.weidu-0.01
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
@pyqtSignature("")
def on_ButtonRight_clicked(self):
"""
Slot documentation goes here.
"""
self.weidu = self.weidu+0.01
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
@pyqtSignature("")
def on_ButtonZoom_clicked(self):
"""
Slot documentation goes here.
"""
if self.zoom < 18:
self.zoom = self.zoom+1
else:
return
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
@pyqtSignature("")
def on_ButtonNarrow_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.zoom > 0:
self.zoom = self.zoom-1
else:
return
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
@pyqtSignature("")
def on_BtnSearch_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
qaddr = self.inputAddress.text().toUtf8()
center = GoogleMapsUtil.getGMapsGEO(qaddr)
self.jindu = float(center[2])
self.weidu = float(center[3])
self.zoom = 14
GmapUrl = GoogleMapsUtil.getGMapsStaticUrl(self.jindu, self.weidu, self.zoom)
self.MapView.setUrl(QtCore.QUrl(GmapUrl))
#QtGui.QListWidgetItem(self.ListAddress)
#self.ListAddress.item(2).setText(QtGui.QApplication.translate("DialogGoogleMaps", "结果3", None, QtGui.QApplication.UnicodeUTF8))
@pyqtSignature("QListWidgetItem*")
def on_ListAddress_itemClicked(self, item):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
pass
@pyqtSignature("")
def on_ListAddress_itemSelectionChanged(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
pass
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
DialogGoogleMaps = GoogleMapsExec()
DialogGoogleMaps.show()
sys.exit(app.exec_())
第五步:这样你运行一下就可以看到最终的成果了.如图.呵呵:
可以在地址框里输入查询的地址,这个程序还没有完善好.怕自己忘记先记录下来吧.等有时间在加以完善.源代码在我的附件里.呵呵
googlemapsapp_pyqt4.zip