WaysAheadGlobal commited on
Commit
17785c0
·
verified ·
1 Parent(s): 51d6e55

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +60 -66
src/streamlit_app.py CHANGED
@@ -2,88 +2,82 @@ import streamlit as st
2
  import streamlit.components.v1 as components
3
 
4
  st.set_page_config(layout="wide")
5
- st.title("🌍 Streamlit with CesiumJS Draggable Marker")
6
 
7
- # HTML and JavaScript code for CesiumJS
8
- html_code = """
9
  <!DOCTYPE html>
10
  <html lang="en">
11
- <head>
12
- <meta charset="utf-8">
13
- <title>CesiumJS Draggable Marker</title>
14
- <script src="https://cesium.com/downloads/cesiumjs/releases/1.104/Build/Cesium/Cesium.js"></script>
15
- <link href="https://cesium.com/downloads/cesiumjs/releases/1.104/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
16
  <style>
17
- html, body, #cesiumContainer {
18
- width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
19
- }
20
- .coordinates {
21
- position: absolute;
22
- bottom: 10px;
23
- left: 10px;
24
- background: rgba(42, 42, 42, 0.8);
25
- padding: 5px;
26
- color: #fff;
27
- font-family: sans-serif;
28
- z-index: 1;
29
- }
30
  </style>
31
- </head>
32
- <body>
33
- <div id="cesiumContainer"></div>
34
- <div class="coordinates" id="coords">Drag the marker to see coordinates here.</div>
35
- <script>
36
- // Initialize the Cesium Viewer
37
- const viewer = new Cesium.Viewer('cesiumContainer', {
 
 
38
  terrainProvider: Cesium.createWorldTerrain()
39
- });
 
 
 
 
40
 
41
- // Add a draggable marker (billboard)
42
- const marker = viewer.entities.add({
43
  position: Cesium.Cartesian3.fromDegrees(55.2708, 25.2048),
44
- billboard: {
45
- image: 'https://cdn-icons-png.flaticon.com/512/684/684908.png',
46
- width: 32,
47
- height: 32
48
  }
49
- });
50
 
51
- // Variables to handle dragging
52
- let isDragging = false;
53
- const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
54
 
55
- // Mouse down event
56
- handler.setInputAction(function(click) {
57
- const pickedObject = viewer.scene.pick(click.position);
58
- if (Cesium.defined(pickedObject) && pickedObject.id === marker) {
59
- isDragging = true;
60
- viewer.scene.screenSpaceCameraController.enableRotate = false;
61
  }
62
- }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
63
 
64
- // Mouse move event
65
- handler.setInputAction(function(movement) {
66
- if (isDragging) {
67
- const cartesian = viewer.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
68
- if (cartesian) {
69
- marker.position = cartesian;
70
- const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
71
- const longitude = Cesium.Math.toDegrees(cartographic.longitude).toFixed(5);
72
- const latitude = Cesium.Math.toDegrees(cartographic.latitude).toFixed(5);
73
- document.getElementById('coords').innerText = `Longitude: ${longitude}, Latitude: ${latitude}`;
74
- }
75
  }
76
- }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
77
 
78
- // Mouse up event
79
- handler.setInputAction(function() {
80
- isDragging = false;
81
  viewer.scene.screenSpaceCameraController.enableRotate = true;
82
- }, Cesium.ScreenSpaceEventType.LEFT_UP);
83
- </script>
84
- </body>
85
  </html>
86
  """
87
 
88
- # Embed the HTML and JavaScript into Streamlit
89
- components.html(html_code, height=700)
 
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)