带有 google map API 和 mySQL 的 Python Web 应用程序

标签 python mysql google-maps google-maps-api-3 web-applications

我正在创建一个关于马萨诸塞州远足路线的网页(用 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/

相关文章:

python - 使用 ruby​​ 或 python 在文件中查找

python - 使用 Python PIL 和 Windows API : how to deal with rounded corners? 的事件窗口屏幕截图

python - python 中的多处理 imap_unordered

python - 如何在 python 中进行阻塞系统调用(并将其结果作为字符串获取)

javascript - 如何将自定义库事件(即 Google map 事件)转换为 RxJS 中的 Observable 流?

mysql - 从不同的 Schemas MySQL 调用过程

php - 更新 NOTORM 中的单个记录

Rails 3 中的 MySQL 语法,不区分大小写的搜索,不适用于 Heroku?

javascript - Google map - Safari 中某些缩放级别出现奇怪的垂直和水平线

javascript - 如何在同一张 map 上使用带有多个标记的谷歌地图 API