VIATEUR-AI commited on
Commit
c698193
·
verified ·
1 Parent(s): a3a2fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -8,7 +8,23 @@ locations = {
8
  "School Zone": {"lat": -1.93, "lon": 30.03}
9
  }
10
 
11
- def simulate_map_google_style():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  markers = ""
13
  alerts = []
14
 
@@ -34,15 +50,19 @@ def simulate_map_google_style():
34
  radius: 10
35
  }}).addTo(map).bindPopup("<b>{name}</b><br>Speed: {speed} km/h<br>Cars: {vehicles}<br>Status: {status}");"""
36
 
 
 
 
37
  html = f"""
38
  <div id="map" style="width:100%; height:500px;"></div>
39
  <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
40
  <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
41
  <script>
42
  var map = L.map('map').setView([-1.935, 30.05], 13);
43
- L.tileLayer('https://{{s}}.basemaps.cartocdn.com/light_all/{{z}}/{{x}}/{{y}}.png', {{
44
- attribution: '&copy; <a href="https://carto.com/">CartoDB</a> contributors',
45
  maxZoom: 19
 
46
  }}).addTo(map);
47
  L.control.zoom({{ position: 'topright' }}).addTo(map);
48
  {markers}
@@ -52,12 +72,14 @@ def simulate_map_google_style():
52
  return "\n".join(alerts), html
53
 
54
  with gr.Blocks() as demo:
55
- gr.Markdown("## 🗺️ Google-Like Traffic Map with Leaflet")
56
 
 
57
  btn = gr.Button("🔁 Refresh Traffic Data")
58
  report = gr.Textbox(label="Traffic Report", lines=8)
59
  map_html = gr.HTML()
60
 
61
- btn.click(fn=simulate_map_google_style, inputs=[], outputs=[report, map_html])
62
 
63
  demo.launch()
 
 
8
  "School Zone": {"lat": -1.93, "lon": 30.03}
9
  }
10
 
11
+ tile_layers = {
12
+ "Light": {
13
+ "url": "https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
14
+ "attribution": '&copy; <a href="https://carto.com/">CartoDB</a> contributors'
15
+ },
16
+ "Dark": {
17
+ "url": "https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
18
+ "attribution": '&copy; <a href="https://carto.com/">CartoDB</a> contributors'
19
+ },
20
+ "Satellite": {
21
+ "url": "https://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
22
+ "attribution": "© Google",
23
+ "subdomains": ["mt0", "mt1", "mt2", "mt3"]
24
+ }
25
+ }
26
+
27
+ def simulate_map_style(style):
28
  markers = ""
29
  alerts = []
30
 
 
50
  radius: 10
51
  }}).addTo(map).bindPopup("<b>{name}</b><br>Speed: {speed} km/h<br>Cars: {vehicles}<br>Status: {status}");"""
52
 
53
+ layer = tile_layers[style]
54
+ subdomains_str = f", subdomains: {layer['subdomains']}" if "subdomains" in layer else ""
55
+
56
  html = f"""
57
  <div id="map" style="width:100%; height:500px;"></div>
58
  <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
59
  <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
60
  <script>
61
  var map = L.map('map').setView([-1.935, 30.05], 13);
62
+ L.tileLayer('{layer['url']}', {{
63
+ attribution: '{layer['attribution']}',
64
  maxZoom: 19
65
+ {subdomains_str}
66
  }}).addTo(map);
67
  L.control.zoom({{ position: 'topright' }}).addTo(map);
68
  {markers}
 
72
  return "\n".join(alerts), html
73
 
74
  with gr.Blocks() as demo:
75
+ gr.Markdown("## 🗺️ Traffic Map with Style Selector (Light, Dark, Satellite)")
76
 
77
+ style_dropdown = gr.Dropdown(label="Hitamo Style ya Map", choices=list(tile_layers.keys()), value="Light")
78
  btn = gr.Button("🔁 Refresh Traffic Data")
79
  report = gr.Textbox(label="Traffic Report", lines=8)
80
  map_html = gr.HTML()
81
 
82
+ btn.click(fn=simulate_map_style, inputs=style_dropdown, outputs=[report, map_html])
83
 
84
  demo.launch()
85
+