LineOfSightViewModel

AMD: require(["esri/widgets/LineOfSight/LineOfSightViewModel"], (LineOfSightViewModel) => { /* code goes here */ });
ESM: import LineOfSightViewModel from "@arcgis/core/widgets/LineOfSight/LineOfSightViewModel.js";
Class: esri/widgets/LineOfSight/LineOfSightViewModel
Inheritance: LineOfSightViewModel Accessor
Since: ArcGIS Maps SDK for JavaScript 4.14

Provides the logic for the LineOfSight widget. Use it to create your own widget or to get access to the observer, targets and intersections.

Using the LineOfSightViewModel you can programmatically set the observer and targets for a line of sight analysis.

// instantiate a new view model
const losViewModel = new LineOfSightViewModel({
 view: view
});

// set an observer
losViewModel.observer = new Point({
  latitude: 42.3521,
  longitude: -71.0559,
  z: 147.139
});

// set a target by passing in a LineOfSightTarget to the targets collection
losViewModel.targets = [new LineOfSightTarget({
   location: new Point({
     latitude: 42.352,
     longitude: -71.051,
     z: 0
   })
})];

// when using JavaScript LineOfSightTarget can be autocast
losViewModel.targets = [{
   location: {
     latitude: 42.352,
     longitude: -71.051,
     z: 0
   }
}];

The view model can also be used to control the different states of the analysis: start() enters the creation state where the user can add the observer and targets. stop() enters the created state where the user can't add new targets but they can still move the observer and existing target.

viewModel.start();

The LineOfSightViewModel also allows you to watch for changes (for example when the user moves the observer or the targets) and gives you access to the intersected graphic and the location where the intersection occurred. The observer is just a Point, so you can simply watch the observer property on the view model to know when an observer gets added or moved.

reactiveUtils.watch(
  () => losViewModel.observer,
  (value) => {
    // do something with the value
  }
);

const highlightHandles = [];

reactiveUtils.watch(
  () => {
    // Watch the whole expression. The callback will be called if any target is added or removed
    // and also if any of the target's `intersectedGraphic` property changes.
    viewModel.targets
      .map((target) => target.intersectedGraphic)
      .map((graphic) => !!graphic)
      .toArray()
  },
  (intersectedGraphics) => {
    // Remove the previous highlights
    highlightHandles.forEach((handle) => handle.remove());

    // Highlight all the intersected graphics
    highlightHandles = intersectedGraphics.map((graphic) => {
      return layerView.highlight(graphic);
    });
  }
);

A target contains information about the location of the target, whether it's visible or not, which graphic it intersects and the location where it intersects that graphic, an integrated mesh or the ground. The LineOfSightTarget class is used to represent a target.

See also

Constructors

LineOfSightViewModel

Constructor
new LineOfSightViewModel(properties)
Parameter
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
Show inherited properties Hide inherited properties
Name Type Summary Class

The line of sight analysis object being created or modified by the view model.

LineOfSightViewModel

The name of the class.

Accessor

The observer's viewpoint from which lines of sight will be drawn towards the targets.

LineOfSightViewModel

The view model's state.

LineOfSightViewModel

A collection of LineOfSightTarget containing the target location and the analysis results.

LineOfSightViewModel

The view from which the widget will operate.

LineOfSightViewModel

Property Details

analysis

Property
analysis LineOfSightAnalysisautocastreadonly
Since: ArcGIS Maps SDK for JavaScript 4.23 LineOfSightViewModel since 4.14, analysis added at 4.23.

The line of sight analysis object being created or modified by the view model.

This property can be set during view model construction, to provide an existing analysis to the view model for modification. Properties on the analysis can also be updated once it's assigned to the view model.

If no analysis is provided during view model construction, the view model automatically creates its own analysis and adds it to the view. In this case, the analysis will also be automatically removed from the view when the view model is destroyed.

Example
// Construct a line of sight analysis object outside of the view model
const analysis = new LineOfSightAnalysis({
  observer: {
    type: "point", // autocasts as new Point()
    x: 7.67,
    y: 45.981,
    z: 3435.765
  },
  targets: [{
    location: {
      type: "point",
      x: 7.659,
      y: 45.976,
      z: 4437
    }
  }]
});

// Ensure that the analysis is added to the view
view.analyses.add(analysis);

// Frame the analysis in the view
view.goTo(analysis.extent);

// Pass the analysis object as a constructor parameter to modify it using the view model
const viewModel = new LineOfSightViewModel({
  analysis: analysis,
  view: view
});

declaredClass

Inherited
Property
declaredClass Stringreadonly
Inherited from Accessor

The name of the class. The declared class name is formatted as esri.folder.className.

observer

Property
observer Point

The observer's viewpoint from which lines of sight will be drawn towards the targets. The Z value of the point is an absolute value.

state

Property
state Stringreadonly

The view model's state.

Value Description
disabled not ready yet
ready ready for analysis
creating observer/target points are being placed
created finished analysis

Possible Values:"disabled" |"ready" |"creating" |"created"

Default Value:disabled

targets

Property
targets Collection<LineOfSightTarget>

A collection of LineOfSightTarget containing the target location and the analysis results.

view

Property
view SceneView

The view from which the widget will operate.

Method Overview

Show inherited methods Hide inherited methods
Name Return Type Summary Class

Adds one or more handles which are to be tied to the lifecycle of the object.

Accessor

Clears the current analysis results.

LineOfSightViewModel

If stopped, this method continues the line of sight analysis and the user can add more targets.

LineOfSightViewModel

Returns true if a named group of handles exist.

Accessor

Removes a group of handles owned by the object.

Accessor

Starts a new line of sight analysis.

LineOfSightViewModel

Stops the current line of sight analysis, keeping the results in the view.

LineOfSightViewModel

Method Details

addHandles

Inherited
Method
addHandles(handleOrHandles, groupKey)
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, addHandles added at 4.25.

Adds one or more handles which are to be tied to the lifecycle of the object. The handles will be removed when the object is destroyed.

// Manually manage handles
const handle = reactiveUtils.when(
  () => !view.updating,
  () => {
    wkidSelect.disabled = false;
  },
  { once: true }
);

this.addHandles(handle);

// Destroy the object
this.destroy();
Parameters
handleOrHandles WatchHandle|WatchHandle[]

Handles marked for removal once the object is destroyed.

groupKey *
optional

Key identifying the group to which the handles should be added. All the handles in the group can later be removed with Accessor.removeHandles(). If no key is provided the handles are added to a default group.

clear

Method
clear()

Clears the current analysis results. After calling this method, the user can set a new observer and targets.

continue

Method
continue()

If stopped, this method continues the line of sight analysis and the user can add more targets.

hasHandles

Inherited
Method
hasHandles(groupKey){Boolean}
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, hasHandles added at 4.25.

Returns true if a named group of handles exist.

Parameter
groupKey *
optional

A group key.

Returns
Type Description
Boolean Returns true if a named group of handles exist.
Example
// Remove a named group of handles if they exist.
if (obj.hasHandles("watch-view-updates")) {
  obj.removeHandles("watch-view-updates");
}

removeHandles

Inherited
Method
removeHandles(groupKey)
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, removeHandles added at 4.25.

Removes a group of handles owned by the object.

Parameter
groupKey *
optional

A group key or an array or collection of group keys to remove.

Example
obj.removeHandles(); // removes handles from default group

obj.removeHandles("handle-group");
obj.removeHandles("other-handle-group");

start

Method
start()

Starts a new line of sight analysis.

stop

Method
stop()

Stops the current line of sight analysis, keeping the results in the view. Users can still interact with existing targets and the observer but they can't place new target points.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.