【问题标题】:Flask Google Maps Infobox with multiple marker images leads to wrong information带有多个标记图像的 Flask Google Maps Infobox 会导致错误信息
【发布时间】:2016-07-19 12:39:25
【问题描述】:

我有一个 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”),但其中一个显示的是付费房间

【问题讨论】:

    标签: python google-maps flask


    【解决方案1】:

    这只是一个猜测——但通过查看显示标记的搜索页面的源代码,我可以看到 var marker_0 和其他的重复项。我假设您正在从零索引迭代 markerlist,然后将 markerlist_bezahlt 的 marker_x 名称从它们的索引位置出发,因此第二个列表会覆盖第一个标记的数据。

    【讨论】:

    • 是的,这正是发生的事情,我现在有了更好的理解,实际上他们都错了。但是如何预防呢?信息框应该适用于两个不同的标记还是?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2014-04-06
    • 2011-02-02
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多