【问题标题】:Python web application with google maps API & my SQL带有谷歌地图 API 和 mySQL 的 Python Web 应用程序
【发布时间】: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


【解决方案1】:

调用函数时需要包含参数。目前您只是在调用 showMap()。您的函数def showMap(lat,lng) 声明了两个必需的参数;经纬度调用时必须提供论文。

我看不到您在哪里声明纬度和经度。您需要在某处获取这些数字并将它们放入函数中。

使用文字的示例调用:
showMap('123','999')

带变量的示例调用:
varLat = 40.44062479999999
varLng = -79.99588640000002
showMap(varLat,varLng)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2011-01-20
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多