import sys
sys.setrecursionlimit(300)
import leafmap.maplibregl as leafmap
import pycafe
import solara
# 獲取 MapTiler 的 API 密鑰
MAPTILER_KEY = pycafe.get_secret("MAPTILER_KEY")
print("I got the MAPTILER_KEY")
class Map(leafmap.Map):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 加載 STAC GUI 工具
#self.add_stac_gui()
# 設置地圖底圖
self.add_basemap("Esri.WorldImagery", visible=True)
# 設定向量資料來源
source = {
"url": f"https://api.maptiler.com/tiles/v3/tiles.json?key={MAPTILER_KEY}",
"type": "vector",
}
# 定義 3D 建築物圖層
layer = {
"id": "3d-buildings",
"source": "openmaptiles",
"source-layer": "building",
"type": "fill-extrusion",
"min-zoom": 15,
"paint": {
"fill-extrusion-color": [
"interpolate",
["linear"],
["get", "render_height"],
0,
"lightgray",
200,
"royalblue",
400,
"lightblue",
],
"fill-extrusion-height": [
"interpolate",
["linear"],
["zoom"],
15,
0,
16,
["get", "render_height"],
],
"fill-extrusion-base": [
"case",
[">=", ["get", "zoom"], 16],
["get", "render_min_height"],
0,
],
},
}
# 添加來源和圖層
self.add_source("openmaptiles", source)
self.add_layer(layer)
self.add_layer_control()
@solara.component
def Page():
# 等待初始化完成
waited = solara.use_reactive(False)
@solara.lab.use_task
async def wait():
import asyncio
await asyncio.sleep(1)
waited.value = True
if waited.value:
solara.Text("before")
# 設置地圖初始視角
Map.element(
center=[-74.0066, 40.7135],
zoom=16,
pitch=45,
bearing=-17,
style="liberty",
height="750px"
)
solara.Text("after")
else:
solara.Text("waiting")