def createFoliumMap(self):
coordinate = (36.1908784, 127.1577341)
m = folium.Map(
title='map',
zoom_start=8,
location=coordinate,
width='100%',
height='100%'
)
js_definition = """
<script>
function removeOtherMarkers(map, clickedMarker) {
var allMarkers = map._layers;
var clickedLat = clickedMarker._latlng.lat;
var clickedLng = clickedMarker._latlng.lng;
for (var markerId in allMarkers) {
var marker = allMarkers[markerId];
if (marker instanceof L.Marker) {
var markerLat = marker._latlng.lat;
var markerLng = marker._latlng.lng;
if (markerLat !== clickedLat || markerLng !== clickedLng) {
map.removeLayer(marker);
}
}
}
}
</script>
"""
m.get_root().html.add_child(folium.Element(js_definition))
for location, popup in zip(self.locations, self.popups):
city_name, info = popup.split(":\n")
html = f"""
<div style="text-align: center;">
<b>{city_name}:</b><br>
<span>{info}</span><br>
<button style="margin-right: 10px; padding: 5px 15px; cursor: pointer;" onclick='removeOtherMarkers(m, this)'>Detail</button>
<button style="margin-top: 10px; padding: 5px 15px; cursor: pointer;" onclick='Select.js'>Select</button>
</div>
"""
iframe = folium.IFrame(html=html, width=250, height=100)
iframe.sandbox = "allow-scripts"
popup = folium.Popup(iframe, max_width=300)
我想实现一项功能,排除大约 8 个 folium 标记中除使用“详细信息”按钮单击的标记之外的所有标记。我尝试通过分离 JavaScript 文件来实现这一点,但失败了。所以,我直接将它添加到HTML代码中。但是,我不断收到“js:Uncaught ReferenceError:removeOtherMarkers 未定义”之类的错误。我不确定问题是出在代码、folium 还是 PyQt 环境。
尝试删除
<script>
标签并使用“m.get_root().script”而不是“m.get_root().html”。
def createFoliumMap(self):
coordinate = (36.1908784, 127.1577341)
m = folium.Map(title='map',
zoom_start=8,
location=coordinate,
width='100%',
height='100%')
# remove '<script>' tags
js_definition = """
function removeOtherMarkers(map, clickedMarker) {
var allMarkers = map._layers;
var clickedLat = clickedMarker._latlng.lat;
var clickedLng = clickedMarker._latlng.lng;
for (var markerId in allMarkers) {
var marker = allMarkers[markerId];
if (marker instanceof L.Marker) {
var markerLat = marker._latlng.lat;
var markerLng = marker._latlng.lng;
if (markerLat !== clickedLat || markerLng !== clickedLng) {
map.removeLayer(marker);
}
}
}
}
"""
# use '.script' instead of '.html'
m.get_root().script.add_child(folium.Element(js_definition))
# ... other code