我有一个Flask应用程序,用户可以通过输入城市名称来搜索城市中的房间。
当输入城市名称时,用户会被重定向到结果页面,我会使用烧瓶谷歌地图在地图上显示标记的可用房间。
我有两个不同的标记图像(免费用户和付费用户):
mymap.markers['static/img/Map-Marker-Marker-Outside-small.png'] = markerlist
mymap.markers['static/img/Map-Marker-Marker-Outside-Pink.png'] = markerlist_bezahlt
我使用列表填充标记,其中存储了所有其他信息,例如:房间和房间的图像,加上它是可点击的,并点击重定向到详细信息页面。
以下是创建地图和标记的整个方法,具体取决于用户是否已付款或是免费的(findroomcity是搜索用户输入的城市名称):
location = geolocator.geocode(findroomcity)
if location.latitude is not None:
mymap = Map(
identifier="view-side",
lat=location.latitude,
lng=location.longitude,
infobox=[],
markers=[],
zoom = 12
)
else:
print "location is none"
all_users = User.query.order_by(desc('bezahlt')).all()
markerlist = []
markerlist_bezahlt = []
for room in all_rooms_in_city:
if room.stadt == findroomcity.lower():
try:
location2 = geolocator.geocode(room.stadt + " " + room.strasse + " " + room.hausnr, timeout=2)
for user in all_users:
if user.id == room.users_id:
if user.bezahlt == False:
markerlist.append((location2.latitude, location2.longitude))
mymap.infobox.append(r'''<a href='/details/''' + str(room.id) + "'>" + r'''<img class='marker-img' src='../static/userimg/''' + room.hauptbild + "'/>")
else:
markerlist_bezahlt.append((location2.latitude, location2.longitude))
mymap.infobox.append(r'''<a href='/details/''' + str(room.id) + "'>" + r'''<img class='marker-img' src='../static/userimg/''' + room.hauptbild + "'/>")
except GeocoderTimedOut as e:
print "in der schleife timeout", e
except AttributeError as e:
print "in der schleife attributeerror", e
mymap.markers['static/img/Map-Marker-Marker-Outside-small.png'] = markerlist
mymap.markers['static/img/Map-Marker-Marker-Outside-Pink.png'] = markerlist_bezahlt
问题是某些标记存储了错误的信息。我想这是因为我分成了两个标记?
以下是heroku的生活示例: Heroku for testing
搜索“Dortmund”并检查两个小蓝色标记(这些都是“FREEUSERZIMMER”),但其中一个显示了付费房间
答案 0 :(得分:0)
这只是一个猜测 - 但是通过查看显示标记的搜索页面的源代码,我可以看到var marker_0
和其他的重复项。我假设您从零索引迭代markerlist
,然后markerlist_bezahlt
将其marker_x
名称基于其索引位置,因此第二个列表会覆盖第一个标记的数据。