我有这段代码,应该通过 Streamlit 快速更新坐标(纬度、经度和 GPS 定位状态):
import socket
import struct
import time
import threading
import streamlit as st
import folium
from streamlit.runtime.scriptrunner import add_script_run_ctx
from streamlit_folium import st_folium
SERVER_ADDRESS = '127.0.0.1'
SERVER_PORT = 25567
LAT_LON_PRECISION = 8
STATUS_NO_FIX = -1 # unable to fix position
STATUS_FIX = 0 # unaugmented fix
STATUS_SBAS_FIX = 1 # with satellite-based augmentation
STATUS_GBAS_FIX = 2
@st.cache_resource
def getSharedData():
return {
'latitude': None,
'longitude': None,
'status': None
}
def receiveData(shared_data):
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.bind((SERVER_ADDRESS, SERVER_PORT))
while True:
data, server_address = client.recvfrom(1024)
unpacked_data = struct.unpack('ddi', data)
latitude, longitude, status = unpacked_data
shared_data['latitude'] = round(latitude, LAT_LON_PRECISION)
shared_data['longitude'] = round(longitude, LAT_LON_PRECISION)
shared_data['status'] = getStatusText(status)
@st.cache_resource
def updateData():
receive_thread = threading.Thread(target=receiveData, args=(getSharedData(),))
receive_thread.daemon = True
add_script_run_ctx(receive_thread)
receive_thread.start()
def getStatusText(status):
if status == STATUS_NO_FIX:
return "No Fix"
elif status == STATUS_FIX:
return "Fix"
elif status == STATUS_SBAS_FIX:
return "SBAS"
elif status == STATUS_GBAS_FIX:
return "GBAS"
return "N/A"
def main():
st.set_page_config(layout="wide")
updateData()
sharedData = getSharedData()
lat_display = st.empty()
lon_display = st.empty()
status_display = st.empty()
map_display = st.empty()
m = folium.Map(zoom_start=12)
while True:
latitude = sharedData['latitude']
longitude = sharedData['longitude']
status = sharedData['status']
if latitude is None or longitude is None:
time.sleep(0.075)
continue
lat_display.text(f"Latitude: {latitude}")
lon_display.text(f"Longitude: {longitude}")
status_display.text(f"Status: {status}")
folium.Marker([latitude, longitude], tooltip="Current Location").add_to(m)
with map_display:
st_folium(m)
time.sleep(0.075)
if __name__ == "__main__":
main()
我终于实现了跨多个客户端的数据共享/缓存。 我现在遇到的问题是我想渲染带有当前位置的地图(以及将来的更多内容)。但是当我执行 st_folium 调用(与 geopandas 和 st.map() 相同)时,它开始闪烁。如何快速更新地图?任何类型的地图,我都不必拘泥于大叶或地理熊猫。
同事的兄弟建议使用plotly_chart。就像魅力一样。