WaysAheadGlobal commited on
Commit
2059ee0
Β·
verified Β·
1 Parent(s): 17785c0

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +64 -57
src/streamlit_app.py CHANGED
@@ -2,82 +2,89 @@ import streamlit as st
2
  import streamlit.components.v1 as components
3
 
4
  st.set_page_config(layout="wide")
5
- st.title("🌍 Dubai 3D Cesium Map with Drag-and-Drop Marker (Ion Enabled)")
6
 
7
- cesium_html = """
8
  <!DOCTYPE html>
9
  <html lang="en">
10
- <head>
11
- <meta charset="utf-8" />
12
- <script src="https://cesium.com/downloads/cesiumjs/releases/1.112/Build/Cesium/Cesium.js"></script>
13
- <link href="https://cesium.com/downloads/cesiumjs/releases/1.112/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
 
14
  <style>
15
- html, body, #cesiumContainer {
16
- width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
17
- }
18
- #dragTip {
19
- position: absolute;
20
- top: 10px;
21
- left: 10px;
22
- z-index: 100;
23
- background: rgba(0,0,0,0.5);
24
- color: white;
25
- padding: 10px;
26
- font-size: 14px;
27
- }
28
  </style>
29
- </head>
30
- <body>
31
- <div id="cesiumContainer"></div>
32
- <div id="dragTip">πŸ–±οΈ Drag the red marker to reposition</div>
33
- <script>
34
- // Set Ion access token
35
- Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0M2VmOTcyMi0wZDIyLTRjMjktOTNlNi0zNjg2MmQyNTFjYTQiLCJpZCI6Mjk3Njk1LCJpYXQiOjE3NDU3NzgyMzh9.0FuHB-PidaNCTRT1sdcp20ufMJ5Z8DEEArhr47BOo4A';
36
 
37
- const viewer = new Cesium.Viewer("cesiumContainer", {
38
  terrainProvider: Cesium.createWorldTerrain()
39
- });
40
 
41
- viewer.camera.flyTo({
 
42
  destination: Cesium.Cartesian3.fromDegrees(55.2708, 25.2048, 15000)
43
- });
44
 
45
- const redPin = viewer.entities.add({
46
- name: "Draggable Marker",
47
  position: Cesium.Cartesian3.fromDegrees(55.2708, 25.2048),
48
- point: {
49
- pixelSize: 15,
50
- color: Cesium.Color.RED,
 
51
  }
52
- });
53
 
54
- let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
55
- let pickedEntity;
56
 
57
- handler.setInputAction(function (click) {
58
- const picked = viewer.scene.pick(click.position);
59
- if (Cesium.defined(picked) && picked.id === redPin) {
60
- pickedEntity = picked.id;
61
- viewer.scene.screenSpaceCameraController.enableRotate = false;
62
  }
63
- }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
64
 
65
- handler.setInputAction(function (movement) {
66
- if (pickedEntity) {
67
- const cartesian = viewer.camera.pickEllipsoid(movement.endPosition);
68
- if (cartesian) {
69
- pickedEntity.position = cartesian;
70
- }
 
 
 
 
71
  }
72
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
73
 
74
- handler.setInputAction(function () {
75
- pickedEntity = null;
76
  viewer.scene.screenSpaceCameraController.enableRotate = true;
77
- }, Cesium.ScreenSpaceEventType.LEFT_UP);
78
- </script>
79
- </body>
80
  </html>
81
  """
82
 
83
- components.html(cesium_html, height=800)
 
2
  import streamlit.components.v1 as components
3
 
4
  st.set_page_config(layout="wide")
5
+ st.title("🌍 Streamlit with CesiumJS – Draggable Marker + Coordinates")
6
 
7
+ html_code = """
8
  <!DOCTYPE html>
9
  <html lang="en">
10
+ <head>
11
+ <meta charset="utf-8">
12
+ <title>CesiumJS Draggable Marker</title>
13
+ <script src="https://cesium.com/downloads/cesiumjs/releases/1.104/Build/Cesium/Cesium.js"></script>
14
+ <link href="https://cesium.com/downloads/cesiumjs/releases/1.104/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
15
  <style>
16
+ html, body, #cesiumContainer {
17
+ width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
18
+ }
19
+ .coordinates {
20
+ position: absolute;
21
+ bottom: 10px;
22
+ left: 10px;
23
+ background: rgba(42, 42, 42, 0.8);
24
+ padding: 5px;
25
+ color: #fff;
26
+ font-family: sans-serif;
27
+ z-index: 1;
28
+ }
29
  </style>
30
+ </head>
31
+ <body>
32
+ <div id="cesiumContainer"></div>
33
+ <div class="coordinates" id="coords">🧭 Drag the marker to see coordinates.</div>
34
+ <script>
35
+ // Set Cesium Ion Access Token
36
+ Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0M2VmOTcyMi0wZDIyLTRjMjktOTNlNi0zNjg2MmQyNTFjYTQiLCJpZCI6Mjk3Njk1LCJpYXQiOjE3NDU3NzgyMzh9.0FuHB-PidaNCTRT1sdcp20ufMJ5Z8DEEArhr47BOo4A';
37
 
38
+ const viewer = new Cesium.Viewer('cesiumContainer', {
39
  terrainProvider: Cesium.createWorldTerrain()
40
+ });
41
 
42
+ // Center on Dubai
43
+ viewer.camera.flyTo({
44
  destination: Cesium.Cartesian3.fromDegrees(55.2708, 25.2048, 15000)
45
+ });
46
 
47
+ // Add a draggable marker
48
+ const marker = viewer.entities.add({
49
  position: Cesium.Cartesian3.fromDegrees(55.2708, 25.2048),
50
+ billboard: {
51
+ image: 'https://cdn-icons-png.flaticon.com/512/684/684908.png',
52
+ width: 32,
53
+ height: 32
54
  }
55
+ });
56
 
57
+ let isDragging = false;
58
+ const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
59
 
60
+ handler.setInputAction(function(click) {
61
+ const pickedObject = viewer.scene.pick(click.position);
62
+ if (Cesium.defined(pickedObject) && pickedObject.id === marker) {
63
+ isDragging = true;
64
+ viewer.scene.screenSpaceCameraController.enableRotate = false;
65
  }
66
+ }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
67
 
68
+ handler.setInputAction(function(movement) {
69
+ if (isDragging) {
70
+ const cartesian = viewer.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
71
+ if (cartesian) {
72
+ marker.position = cartesian;
73
+ const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
74
+ const longitude = Cesium.Math.toDegrees(cartographic.longitude).toFixed(5);
75
+ const latitude = Cesium.Math.toDegrees(cartographic.latitude).toFixed(5);
76
+ document.getElementById('coords').innerText = `πŸ“ Longitude: ${longitude}, Latitude: ${latitude}`;
77
+ }
78
  }
79
+ }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
80
 
81
+ handler.setInputAction(function() {
82
+ isDragging = false;
83
  viewer.scene.screenSpaceCameraController.enableRotate = true;
84
+ }, Cesium.ScreenSpaceEventType.LEFT_UP);
85
+ </script>
86
+ </body>
87
  </html>
88
  """
89
 
90
+ components.html(html_code, height=700)