Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Alfakhori
Gengenbach
Commits
d2df398a
Commit
d2df398a
authored
Sep 29, 2026
by
Alfakhori
Browse files
optimisation
parent
6d8f61ae
Pipeline
#12467
passed with stage
in 26 seconds
Changes
2
Pipelines
1
Expand all
Show whitespace changes
Inline
Side-by-side
public/ar_main.js
View file @
d2df398a
This diff is collapsed.
Click to expand it.
public/ar_overviewmap.js
View file @
d2df398a
...
...
@@ -4,6 +4,18 @@ let userIcon;
let
intervalId
;
let
modeMarkers
=
[];
// PERFORMANCE: Reuse one Vector3 instead of allocating a new
// Vector3 every time updateMap() runs.
let
cameraPosition
=
new
THREE
.
Vector3
();
// PERFORMANCE: Cache the map window element once.
let
mapWindow
=
document
.
getElementById
(
"
map-window
"
);
// PERFORMANCE: Remember the last mapped camera position so Leaflet
// is updated only after the user has actually moved.
let
lastMapX
=
null
;
let
lastMapZ
=
null
;
// ============================================================
// Proposed plan overlay
// ============================================================
...
...
@@ -24,253 +36,141 @@ let planVisible = false;
// Longitude = 8.010410216
const
planBounds
=
[
[
48.405858385
,
7.99708007
0
],
// Southwest / bottom-left
[
48.414966828
,
8.010410216
]
// Northeast / top-right
[
48.405858385
,
7.99708007
],
[
48.414966828
,
8.010410216
]
,
];
const
earthRadius
=
6378137
;
// Erdradius in Metern (WGS84)
const
earthRadius
=
6378137
;
// ============================================================
// Initialize map
// ============================================================
function
init_map
()
{
const
mapContainer
=
document
.
getElementById
(
"
map-container
"
);
if
(
mapContainer
)
{
if
(
!
init
)
{
// ====================================================
// Create Leaflet map
// ====================================================
mymap
=
L
.
map
(
mapContainer
);
// ====================================================
// OpenStreetMap Germany basemap
// ====================================================
var
OpenStreetMap_DE
=
L
.
tileLayer
(
'
https://tile.openstreetmap.de/{z}/{x}/{y}.png
'
,
"
https://tile.openstreetmap.de/{z}/{x}/{y}.png
"
,
{
//maxZoom: 20,
attribution
:
'
© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors
'
}
'
© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors
'
,
}
,
);
OpenStreetMap_DE
.
addTo
(
mymap
);
// ====================================================
// User icon
// ====================================================
userIcon
=
L
.
icon
({
iconUrl
:
'
assets/previewImages/map-user.png
'
,
iconUrl
:
"
assets/previewImages/map-user.png
"
,
iconSize
:
[
32
,
32
],
iconAnchor
:
[
16
,
32
],
popupAnchor
:
[
0
,
-
32
]
popupAnchor
:
[
0
,
-
32
]
,
});
// ====================================================
// Initial map position
// ====================================================
//
// The map is centered on the proposed plan instead
// of the user's current GPS position.
//
// We use fitBounds() so the whole plan is visible.
//
mymap
.
fitBounds
(
planBounds
,
{
padding
:
[
20
,
20
]
padding
:
[
20
,
20
]
,
});
// ====================================================
// User position marker
// ====================================================
userIcon
=
L
.
marker
(
[
geoLocation
.
latitude
,
geoLocation
.
longitude
],
{
icon
:
userIcon
}
)
.
bindPopup
(
'
Ihre Position
'
)
userIcon
=
L
.
marker
([
geoLocation
.
latitude
,
geoLocation
.
longitude
],
{
icon
:
userIcon
,
})
.
bindPopup
(
"
Ihre Position
"
)
.
addTo
(
mymap
);
init
=
true
;
}
// ========================================================
// Remove old model markers
// ========================================================
modeMarkers
.
forEach
(
marker
=>
{
modeMarkers
.
forEach
(
(
marker
)
=>
{
marker
.
remove
();
});
// Clear marker array
modeMarkers
=
[];
// ========================================================
// Add currently placed models to map
// ========================================================
const
models
=
getAllPlacedModels
();
models
.
forEach
(
model
=>
{
models
.
forEach
((
model
)
=>
{
const
{
lat
,
lng
}
=
threeToLeaflet
(
model
.
position
.
x
,
model
.
position
.
z
);
const
{
lat
,
lng
}
=
threeToLeaflet
(
model
.
position
.
x
,
model
.
position
.
z
);
const
modelMarker
=
L
.
marker
(
[
lat
,
lng
],
{
const
modelMarker
=
L
.
marker
([
lat
,
lng
],
{
icon
:
L
.
icon
({
iconUrl
:
model
.
modelConfig
.
image
,
iconSize
:
[
16
,
16
],
iconAnchor
:
[
16
,
32
],
popupAnchor
:
[
0
,
-
32
]
popupAnchor
:
[
0
,
-
32
],
}),
})
}
)
.
addTo
(
mymap
)
.
bindPopup
(
model
.
modelConfig
?.
name
||
"
Unbenannt
"
);
.
bindPopup
(
model
.
modelConfig
?.
name
||
"
Unbenannt
"
);
modeMarkers
.
push
(
modelMarker
);
});
// ========================================================
// Add proposed plan
// ========================================================
addPlanOverlay
();
// ========================================================
// Make sure Leaflet knows the map container dimensions
// ========================================================
setTimeout
(()
=>
{
centerOnPlan
();
},
100
);
// ========================================================
// Update map position continuously
// ========================================================
// PERFORMANCE: Start only one map update interval.
if
(
!
intervalId
)
{
intervalId
=
setInterval
(
updateMap
,
500
);
}
}
}
// ============================================================
// Center map on proposed plan
// ============================================================
function
centerOnPlan
()
{
if
(
!
mymap
)
{
console
.
error
(
"
Map ist noch nicht initialisiert.
"
);
console
.
error
(
"
Map ist noch nicht initialisiert.
"
);
return
;
}
// Tell Leaflet to recalculate the map container size.
// This is important because map-window may initially
// be hidden when the map is initialized.
mymap
.
invalidateSize
();
// Center and zoom the map on the proposed plan.
mymap
.
fitBounds
(
planBounds
,
{
padding
:
[
20
,
20
]
}
);
mymap
.
fitBounds
(
planBounds
,
{
padding
:
[
20
,
20
],
});
}
// ============================================================
// Add georeferenced proposed plan
// ============================================================
//
// Original coordinate system:
// DHDN / Gauss-Krüger Zone 3
// EPSG:31467
//
// Image extent:
//
// Southwest:
// X = 3425822.00
// Y = 5363602.00
//
// Northeast:
// X = 3426822.00
// Y = 5364602.00
//
// Converted to WGS84:
//
// Southwest:
// Latitude = 48.405858385
// Longitude = 7.997080070
//
// Northeast:
// Latitude = 48.414966828
// Longitude = 8.010410216
//
// Image:
//
// assets/plans/proposed-plan.png
// ============================================================
function
addPlanOverlay
()
{
if
(
!
mymap
)
{
console
.
error
(
"
Map ist noch nicht initialisiert.
"
);
console
.
error
(
"
Map ist noch nicht initialisiert.
"
);
return
;
}
// ========================================================
// If overlay already exists, show it
// ========================================================
if
(
planOverlay
)
{
planOverlay
.
addTo
(
mymap
);
planVisible
=
true
;
...
...
@@ -278,45 +178,25 @@ function addPlanOverlay() {
return
;
}
// ========================================================
// Create image overlay
// ========================================================
planOverlay
=
L
.
imageOverlay
(
'
assets/plans/proposed-plan.png
'
,
planBounds
,
{
// Transparency
planOverlay
=
L
.
imageOverlay
(
"
assets/plans/proposed-plan.png
"
,
planBounds
,
{
opacity
:
0.7
,
// The plan does not capture mouse/touch events
interactive
:
false
}
);
// ========================================================
// Add overlay to map
// ========================================================
interactive
:
false
,
});
planOverlay
.
addTo
(
mymap
);
planVisible
=
true
;
}
// ============================================================
// Update map
// ============================================================
function
updateMap
()
{
if
(
document
.
getElementById
(
"
map-window
"
).
style
.
display
===
'
none
'
&&
intervalId
)
{
// The interval is normally stopped by closeMap().
// This guard also handles cases where the map is hidden
// by another UI action.
if
(
mapWindow
&&
mapWindow
.
style
.
display
===
"
none
"
&&
intervalId
)
{
clearInterval
(
intervalId
);
intervalId
=
null
;
...
...
@@ -324,176 +204,121 @@ function updateMap() {
return
;
}
if
(
!
camera
||
!
userIcon
)
{
return
;
}
let
vec
=
new
THREE
.
Vector3
();
const
userPosition
=
camera
.
getWorldPosition
(
vec
);
// PERFORMANCE: Reuse the same Vector3 object.
camera
.
getWorldPosition
(
cameraPosition
);
const
{
lat
,
lng
}
=
threeToLeaflet
(
userPosition
.
x
,
userPosition
.
z
);
// PERFORMANCE: Do not update Leaflet if the user
// has not moved.
if
(
lastMapX
!==
null
&&
Math
.
abs
(
cameraPosition
.
x
-
lastMapX
)
<
0.1
&&
Math
.
abs
(
cameraPosition
.
z
-
lastMapZ
)
<
0.1
)
{
return
;
}
lastMapX
=
cameraPosition
.
x
;
lastMapZ
=
cameraPosition
.
z
;
if
(
userIcon
)
{
const
{
lat
,
lng
}
=
threeToLeaflet
(
cameraPosition
.
x
,
cameraPosition
.
z
);
userIcon
.
setLatLng
([
lat
,
lng
]);
}
userIcon
.
setLatLng
([
lat
,
lng
]);
}
// ============================================================
// Convert Three.js coordinates to Leaflet coordinates
// ============================================================
function
threeToLeaflet
(
threeX
,
threeZ
)
{
if
(
!
geoLocation
)
{
console
.
error
(
"
GeoLocation ist noch nicht verfügbar
"
);
console
.
error
(
"
GeoLocation ist noch nicht verfügbar
"
);
return
;
}
const
originLat
=
geoLocation
.
latitude
;
const
originLng
=
geoLocation
.
longitude
;
const
deltaLat
=
(
threeZ
/
earthRadius
)
*
(
180
/
Math
.
PI
);
const
originLng
=
geoLocation
.
longitude
;
const
deltaLat
=
(
threeZ
/
earthRadius
)
*
(
180
/
Math
.
PI
);
const
deltaLng
=
(
threeX
/
(
earthRadius
*
Math
.
cos
(
Math
.
PI
*
originLat
/
180
)
)
)
*
(
threeX
/
(
earthRadius
*
Math
.
cos
((
Math
.
PI
*
originLat
)
/
180
)))
*
(
180
/
Math
.
PI
);
const
globalLat
=
originLat
+
deltaLat
;
const
globalLat
=
originLat
+
deltaLat
;
const
globalLng
=
originLng
+
deltaLng
;
const
globalLng
=
originLng
+
deltaLng
;
return
{
lat
:
globalLat
,
lng
:
globalLng
lng
:
globalLng
,
};
}
// ============================================================
// Convert Leaflet coordinates to Three.js coordinates
// ============================================================
function
leafletToThree
(
lat
,
lng
)
{
if
(
!
geoLocation
)
{
console
.
error
(
"
GeoLocation ist noch nicht verfügbar
"
);
console
.
error
(
"
GeoLocation ist noch nicht verfügbar
"
);
return
;
}
const
originLat
=
geoLocation
.
latitude
;
const
originLng
=
geoLocation
.
longitude
;
const
deltaLat
=
lat
-
originLat
;
const
deltaLng
=
lng
-
originLng
;
const
originLng
=
geoLocation
.
longitude
;
const
deltaLat
=
lat
-
originLat
;
const
threeZ
=
deltaLat
*
(
Math
.
PI
/
180
)
*
earthRadius
;
const
deltaLng
=
lng
-
originLng
;
const
threeZ
=
deltaLat
*
(
Math
.
PI
/
180
)
*
earthRadius
;
const
threeX
=
deltaLng
*
(
Math
.
PI
/
180
)
*
earthRadius
*
Math
.
cos
(
Math
.
PI
*
originLat
/
180
);
Math
.
cos
((
Math
.
PI
*
originLat
)
/
180
);
return
{
x
:
threeX
,
z
:
threeZ
z
:
threeZ
,
};
}
// ============================================================
// Round number
// ============================================================
function
roundTo
(
n
,
digits
)
{
var
negative
=
false
;
if
(
digits
===
undefined
)
{
digits
=
0
;
}
if
(
n
<
0
)
{
negative
=
true
;
n
=
n
*
-
1
;
}
var
multiplicator
=
Math
.
pow
(
10
,
digits
);
var
multiplicator
=
Math
.
pow
(
10
,
digits
);
n
=
parseFloat
(
(
n
*
multiplicator
).
toFixed
(
11
)
);
n
=
(
Math
.
round
(
n
)
/
multiplicator
).
toFixed
(
digits
);
n
=
parseFloat
((
n
*
multiplicator
).
toFixed
(
11
));
n
=
(
Math
.
round
(
n
)
/
multiplicator
).
toFixed
(
digits
);
if
(
negative
)
{
n
=
(
n
*
-
1
).
toFixed
(
digits
);
n
=
(
n
*
-
1
).
toFixed
(
digits
);
}
return
parseFloat
(
n
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment