This sample demonstrates how to use the LineOfSight widget to check whether one or multiple targets are visible from an observer viewpoint in a SceneView.
In this sample, the observer is set on the upper floor of an existing building. The sample shows how the view of the observer is obstructed if a new building is constructed in front of it. Turn the development layer on and off to see how the view of the river gets obstructed. Move the observer or the targets to explore points where the view is not obstructed. Click on Continue analysis
to add more targets to the analysis.
The widget can be added to the scene using the following code snippet:
const lineOfSight = new LineOfSight({
view: view
});
view.ui.add(lineOfSight, "top-right");
The LineOfSightViewModel can be used to initially add an observer and several target points.
const viewModel = lineOfSight.viewModel;
viewModel.observer = new Point({
latitude: 42.3521,
longitude: -71.0559,
z: 147.139
});
viewModel.targets = [
createTarget(42.3492, -71.0529),
createTarget(42.3477, -71.0542)
];
These properties can also be used to watch for changes like adding, moving or removing targets or the observer. Remove a target with right click.
// watch when the observer changes
reactiveUtils.watch(() => viewModel.observer, () => {
setIntersectionMarkers();
});
// watch when a new target is added or removed
viewModel.targets.on("change", (event) => {
event.added.forEach((target) => {
setIntersectionMarkers();
// for each target watch when the intersection changes
reactiveUtils.watch(() => target.intersectedLocation, () => {
setIntersectionMarkers();
});
event.removed.forEach(() => {
// remove intersection markers for removed targets (remove with right click on target)
setIntersectionMarkers();
});
});
});
That LineOfSightTarget class gives access to analysis results like the location of the intersection or the graphic that obstructs the view from the observer to the target.
viewModel.targets.forEach((target) => {
if (target.intersectedLocation) {
const graphic = new Graphic({
symbol: intersectionSymbol,
geometry: target.intersectedLocation
});
view.graphics.add(graphic);
}
});