首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否将文本添加到Folium FastMarkerCluster标记?

是否将文本添加到Folium FastMarkerCluster标记?
EN

Stack Overflow用户
提问于 2018-06-03 05:28:29
回答 2查看 4.5K关注 0票数 2

使用下面的代码,我尝试将文本添加到下面的folium FastMarkerCluster markers.The代码中,生成了一个地图,但没有成功添加文本。文本被包含为对应于每个lon lat对的字符串。

我认为这个问题与包含text列的df是一个pandas序列对象有关。据我所知,在这种情况下应该如何指定‘df.column should’。然而,这并不管用。我还尝试过将该列转换为列表,但这种方法也没有成功。任何指导者都非常感谢。

代码语言:javascript
复制
xlat = guns2013['latitude'].tolist()
xlon = guns2013['longitude'].tolist()
locations = list(zip(xlat, xlon))
map2 = folium.Map(location=[38.9, -77.05], tiles='CartoDB dark_matter', 
zoom_start=1)
marker_cluster = MarkerCluster().add_to(map2)
for point in range(0, len(locations)):
   folium.Marker(locations[point], 
   popup='guns2013.texts'[point]).add_to(marker_cluster)

map2
EN

回答 2

Stack Overflow用户

发布于 2019-07-27 01:01:06

我尝试了一下,最终您可以将弹出窗口从python传递给callback函数,如下所示:

代码语言:javascript
复制
some_map = folium.Map(location=[df['latitude'].mean(), 
   df['longitude'].mean()], 
 zoom_start=9)
               
callback = ('function (row) {' 
                'var marker = L.marker(new L.LatLng(row[0], row[1]), {color: "red"});'
                'var icon = L.AwesomeMarkers.icon({'
                "icon: 'info-sign',"
                "iconColor: 'white',"
                "markerColor: 'green',"
                "prefix: 'glyphicon',"
                "extraClasses: 'fa-rotate-0'"
                    '});'
                'marker.setIcon(icon);'
                "var popup = L.popup({maxWidth: '300'});"
                "const display_text = {text: row[2]};"
                "var mytext = $(`<div id='mytext' class='display_text' style='width: 100.0%; height: 100.0%;'> ${display_text.text}</div>`)[0];"
                "popup.setContent(mytext);"
                "marker.bindPopup(popup);"
                'return marker};')
                             
some_map.add_child(FastMarkerCluster(df[['latitude', 'longitude','use_code_name']].values.tolist(), callback=callback))

票数 4
EN

Stack Overflow用户

发布于 2018-06-03 05:44:40

果然,答案来了。也许这对面临同样问题的人有帮助。如果文本没有映射,请检查包含它的df是否不是pandas系列对象,如果是,则使用第一行转换为pandas df。剩下的就行了。

代码语言:javascript
复制
df_text = df_old[['texts']]

xlat = guns2013['latitude'].tolist()
xlon = guns2013['longitude'].tolist()
locations = list(zip(xlat, xlon))
map2 = folium.Map(location=[38.9, -77.05], tiles='CartoDB dark_matter', 
zoom_start=1)
marker_cluster = MarkerCluster().add_to(map2)
try:
   for point in range(0, len(locations)):
   folium.Marker(locations[point], popup = folium.Popup(df_text['texts'] 
   [point])).add_to(marker_cluster)    
except:
   pass
map2
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50661316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档