我正在创建一个关于马萨诸塞州远足路线的网页(用 Python 编写)。我希望能够单击路线名称,并让我的网页显示该位置的谷歌地图。在我的数据库中,我有两列,分别是纬度和经度。我对 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()。您的函数 def showMap(lat,lng)
声明了两个必需参数;纬度和经度。调用时必须提供论文。
我不明白你在哪里声明纬度和经度。您需要在某处获取这些数字并将它们放入函数中。
带文字的调用示例:
showMap('123','999')
带变量的调用示例:
varLat = 40.44062479999999
varLng = -79.99588640000002
showMap(varLat,varLng)
关于带有 google map API 和 mySQL 的 Python Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36847453/