【发布时间】:2016-08-19 06:00:18
【问题描述】:
我正在马萨诸塞州创建一个远足径网页(用 Python 编写)。我希望能够单击路径的名称,并让我的网页显示该位置的谷歌地图。在我的数据库中,我有 2 列,用于纬度和经度。我对 Python 很陌生,真的不知道该怎么做。这是我写的代码:
def getMap():
"""
This is a middleware function which returns
latitude and longitude coordinates
from our database.
"""
# connect to db
conn, cursor = getConnectionAndCursor()
# prepare SQL
sql = """
SELECT lat
AND lng
FROM hiking
WHERE name = %s
"""
parameters=(name,)
# run the SQL
cursor.execute(sql, name)
# fetch the results
data = cursor.fetchall()
# clean up
cursor.close()
conn.close()
return data
def showMap(lat,lng):
"""
Presentation layer function to display a Google map.
"""
## create an HTML table for output:
print"""
<script
src="http://maps.googleapis.com/maps/api/js">
</script>
<script>
function initialize() {
var mapProp = {
center:new google.maps.LatLng(%s,%s),
zoom:15,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"), mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<div id="googleMap" style="width:500px;height:380px;"></div>
""" % (lat, lng)
if __name__ == "__main__":
# get form field data
form = cgi.FieldStorage()
debugFormData(form)
doHTMLHead("MassHike: A database of popular Massachusetts trails")
if 'name' in form:
name=form['name'].value
mapdata = getMap()
showMap()
else:
data = getAllHikes()
showAllHikes(data)
doHTMLTail()
今天我向助教求助,她不知道该怎么办。我的网页给我一个错误说':showMap()正好需要2个参数(0给定)',所以我做错了。任何建议将不胜感激!
【问题讨论】:
-
更新:没有注意到 showMap 函数是空的。输入参数,现在我得到这个错误 '
: name 'lat' is not defined'
标签: python mysql google-maps google-maps-api-3 web-applications