Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ treasures = [
|
|
| 7 |
{"name": "寶藏第3個", "lat": 24.956398, "lon": 121.297729},
|
| 8 |
]
|
| 9 |
|
| 10 |
-
found = set() # 儲存已找到寶藏的 index
|
| 11 |
-
|
| 12 |
def haversine(lat1, lon1, lat2, lon2):
|
| 13 |
R = 6371000
|
| 14 |
phi1, phi2 = math.radians(lat1), math.radians(lat2)
|
|
@@ -18,19 +16,20 @@ def haversine(lat1, lon1, lat2, lon2):
|
|
| 18 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
|
| 19 |
return R * c
|
| 20 |
|
| 21 |
-
def check_nearest(lat, lon):
|
| 22 |
if not lat or not lon:
|
| 23 |
-
return "❌ 請先取得 GPS", "", ""
|
| 24 |
|
| 25 |
try:
|
| 26 |
lat = float(lat)
|
| 27 |
lon = float(lon)
|
| 28 |
except:
|
| 29 |
-
return "❌ 座標格式錯誤", "", ""
|
| 30 |
|
| 31 |
nearest = None
|
| 32 |
nearest_idx = None
|
| 33 |
min_distance = float("inf")
|
|
|
|
| 34 |
for idx, treasure in enumerate(treasures):
|
| 35 |
dist = haversine(lat, lon, treasure["lat"], treasure["lon"])
|
| 36 |
if dist < min_distance:
|
|
@@ -38,14 +37,11 @@ def check_nearest(lat, lon):
|
|
| 38 |
nearest = treasure
|
| 39 |
nearest_idx = idx
|
| 40 |
|
| 41 |
-
if nearest is None:
|
| 42 |
-
return "❌ 無寶藏", "", ""
|
| 43 |
-
|
| 44 |
dist = haversine(lat, lon, nearest["lat"], nearest["lon"])
|
| 45 |
gmap = f"https://www.google.com/maps/dir/{lat},{lon}/{nearest['lat']},{nearest['lon']}"
|
| 46 |
|
| 47 |
if dist <= 30 and nearest_idx not in found:
|
| 48 |
-
found.
|
| 49 |
status = f"🎯 找到 {nearest['name']}!距離:{dist:.2f} 公尺 ✅"
|
| 50 |
html = f"""
|
| 51 |
<h3>{nearest['name']} 🎉 已找到!</h3>
|
|
@@ -65,7 +61,7 @@ def check_nearest(lat, lon):
|
|
| 65 |
"""
|
| 66 |
|
| 67 |
progress = f"✅ 已找到:{', '.join([treasures[i]['name'] for i in sorted(found)])}" if found else ""
|
| 68 |
-
return status, html, progress
|
| 69 |
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown("# 🗺️ GPS 尋寶遊戲")
|
|
@@ -101,8 +97,10 @@ with gr.Blocks() as demo:
|
|
| 101 |
status = gr.Markdown()
|
| 102 |
link = gr.HTML()
|
| 103 |
progress = gr.Markdown()
|
|
|
|
| 104 |
|
| 105 |
btn = gr.Button("🔍 檢查距離 + 導航")
|
| 106 |
-
btn.click(fn=check_nearest, inputs=[lat, lon
|
|
|
|
| 107 |
|
| 108 |
demo.launch()
|
|
|
|
| 7 |
{"name": "寶藏第3個", "lat": 24.956398, "lon": 121.297729},
|
| 8 |
]
|
| 9 |
|
|
|
|
|
|
|
| 10 |
def haversine(lat1, lon1, lat2, lon2):
|
| 11 |
R = 6371000
|
| 12 |
phi1, phi2 = math.radians(lat1), math.radians(lat2)
|
|
|
|
| 16 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
|
| 17 |
return R * c
|
| 18 |
|
| 19 |
+
def check_nearest(lat, lon, found):
|
| 20 |
if not lat or not lon:
|
| 21 |
+
return "❌ 請先取得 GPS", "", "", found
|
| 22 |
|
| 23 |
try:
|
| 24 |
lat = float(lat)
|
| 25 |
lon = float(lon)
|
| 26 |
except:
|
| 27 |
+
return "❌ 座標格式錯誤", "", "", found
|
| 28 |
|
| 29 |
nearest = None
|
| 30 |
nearest_idx = None
|
| 31 |
min_distance = float("inf")
|
| 32 |
+
|
| 33 |
for idx, treasure in enumerate(treasures):
|
| 34 |
dist = haversine(lat, lon, treasure["lat"], treasure["lon"])
|
| 35 |
if dist < min_distance:
|
|
|
|
| 37 |
nearest = treasure
|
| 38 |
nearest_idx = idx
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
dist = haversine(lat, lon, nearest["lat"], nearest["lon"])
|
| 41 |
gmap = f"https://www.google.com/maps/dir/{lat},{lon}/{nearest['lat']},{nearest['lon']}"
|
| 42 |
|
| 43 |
if dist <= 30 and nearest_idx not in found:
|
| 44 |
+
found.append(nearest_idx)
|
| 45 |
status = f"🎯 找到 {nearest['name']}!距離:{dist:.2f} 公尺 ✅"
|
| 46 |
html = f"""
|
| 47 |
<h3>{nearest['name']} 🎉 已找到!</h3>
|
|
|
|
| 61 |
"""
|
| 62 |
|
| 63 |
progress = f"✅ 已找到:{', '.join([treasures[i]['name'] for i in sorted(found)])}" if found else ""
|
| 64 |
+
return status, html, progress, found
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("# 🗺️ GPS 尋寶遊戲")
|
|
|
|
| 97 |
status = gr.Markdown()
|
| 98 |
link = gr.HTML()
|
| 99 |
progress = gr.Markdown()
|
| 100 |
+
found_state = gr.State([])
|
| 101 |
|
| 102 |
btn = gr.Button("🔍 檢查距離 + 導航")
|
| 103 |
+
btn.click(fn=check_nearest, inputs=[lat, lon, found_state],
|
| 104 |
+
outputs=[status, link, progress, found_state])
|
| 105 |
|
| 106 |
demo.launch()
|