Accessor

AMD: require(["geoscene/core/Accessor"], (Accessor) => { /* code goes here */ });
ESM: import Accessor from "@geoscene/core/core/Accessor";
类: geoscene/core/Accessor
子类:ActionBase , ActiveLayerInfo , AddressCandidate , AddressCandidate , AggregatedGeometry , ApplicationProperties , ApplicationProperties , AreaMeasurement3DViewModel , AreasAndLengthsParameters , AreasAndLengthsParameters , Association , AssociationGeometriesResult , AttachmentInfo , AttachmentQuery , AttachmentQuery , AttachmentsViewModel , AttributeColorInfo , AttributionViewModel , AuthoringInfo , AuthoringInfoVisualVariable , Background , BarChartMediaInfo , BarcodeScannerInput , Basemap , BasemapGalleryItem , BasemapGalleryViewModel , BasemapLayerListViewModel , BasemapToggleViewModel , BasemapView , Bookmark , BookmarksViewModel , BufferParameters , BufferParameters , BuildingComponentSublayerView , BuildingExplorerViewModel , BuildingFilter , BuildingLevel , BuildingPhase , BuildingSublayer , BuildingSummaryStatistics , ButtonMenuItem , ButtonMenuViewModel , Callout3D , Camera , ChartMediaInfoValue , ChartMediaInfoValueSeries , ClassBreakInfo , ClosestFacilityParameters , ClosestFacilityParameters , ClosestFacilitySolveResult , ClosestFacilitySolveResult , CloudyWeather , Collection , ColorBackground , ColorRamp , ColorRamp , ColorSizeStop , ColorStop , ColormapInfo , ComboBoxInput , CompassViewModel , Content , Conversion , CoordinateConversionViewModel , CreateFeaturesWorkflowData , CreateWorkflowData , Credential , CustomTemplate , DataFile , DataFile , DataLayer , DataLayer , DatePickerViewModel , DateTimePickerInput , DaylightViewModel , DensifyParameters , DensifyParameters , DimensionalDefinition , DirectLineMeasurement3DViewModel , DirectionLine , DirectionPoint , DirectionsFeatureSet , DirectionsFeatureSet , DirectionsViewModel , DistanceMeasurement2DViewModel , DistanceParameters , DistanceParameters , Domain , Draw , DrawAction , Edges3D , EditorViewModel , Edits , Element , ElementExpressionInfo , ElevationProfileLine , ElevationProfileViewModel , Environment , ExpandViewModel , ExpressionInfo , ExpressionInfo , FacilityLayerInfo , FeatureEffect , FeatureEffect , FeatureFilter , FeatureFilter , FeatureFormViewModel , FeatureReductionCluster , FeatureReductionSelection , FeatureSet , FeatureSet , FeatureSnappingLayerSource , FeatureTableViewModel , FeatureTemplate , FeatureTemplatesViewModel , FeatureType , FeatureViewModel , Field , FieldColumn , FieldConfig , FieldGroupConfig , FieldInfo , FieldInfoFormat , FindParameters , FindParameters , FindResult , FindResult , FloorFilterViewModel , FlowRenderer , FoggyWeather , Font , FormTemplate , Format , FullscreenViewModel , FunctionResult , GPMessage , GPMessage , GamepadInputDevice , GamepadSettings , GamepadSettings , GeneralizeParameters , GeneralizeParameters , Geometry , GeometryFieldsInfo , Graphic , Ground , GroundView , HandleOwner , Handles , HeatmapColorStop , HeightModelInfo , HistogramViewModel , HomeViewModel , IdentifyParameters , IdentifyParameters , IdentifyResult , IdentifyResult , ImageAngleParameters , ImageAngleResult , ImageHistogramParameters , ImageHistogramParameters , ImageIdentifyParameters , ImageIdentifyParameters , ImageIdentifyResult , ImageIdentifyResult , ImageMediaInfo , ImageMediaInfoValue , ImageParameters , ImagePixelLocationParameters , ImagePixelLocationResult , ImageSample , ImageSampleParameters , ImageSampleResult , InitialViewProperties , InitialViewProperties , Input , InputField , InputFieldGroup , JobInfo , JobInfo , KMLSublayer , LOD , LabelClass , Layer , LayerFloorInfo , LayerListViewModel , LayerOptions , LayerView , LegendLayer , LegendLayer , LegendViewModel , LengthsParameters , LengthsParameters , LevelLayerInfo , Lighting , LineChartMediaInfo , LineOfSightTarget , LineOfSightViewModel , LineStyleMarker3D , LineStylePattern3D , LineSymbolMarker , LinearUnit , LinearUnit , ListItem , LocalBasemapsSource , LocateViewModel , Magnifier , Map , MapFloorInfo , MapImage , MeasurementViewModel , MeshComponent , MeshMaterial , MeshTexture , MosaicRule , NAMessage , NAMessage , Navigation , NavigationToggleViewModel , Network , NetworkElement , NetworkFeatureSet , NetworkUrl , OAuthInfo , OffsetParameters , OffsetParameters , OpacityStop , ParameterValue , ParameterValue , PieChartMediaInfo , PixelBlock , PointBarrier , PointCloudFilter , PointCloudRenderer , PolygonBarrier , PolylineBarrier , PopupTemplate , PopupViewModel , Portal , PortalFolder , PortalGroup , PortalItem , PortalItemResource , PortalQueryParams , PortalQueryResult , PortalRating , PortalUser , Presentation , PrintParameters , PrintParameters , PrintTemplate , PrintTemplate , PrintViewModel , ProjectParameters , ProjectParameters , Query , Query , RadioButtonsInput , RainyWeather , RasterColormapRenderer , RasterData , RasterData , RasterFunction , RasterInfo , RasterShadedReliefRenderer , RasterStretchRenderer , RelatedRecordsInfo , RelatedRecordsInfoFieldOrder , RelationParameters , RelationParameters , Relationship , RelationshipQuery , RelationshipQuery , Renderer , RouteInfo , RouteParameters , RouteParameters , RouteResult , RouteResult , RouteSolveResult , RouteSolveResult , ScaleBarViewModel , ScaleRangeSliderViewModel , ScaleRanges , SceneModification , Search , SearchLayer , SearchLayerField , SearchSource , SearchTable , SearchTableField , SearchViewModel , ServerInfo , ServiceAreaParameters , ServiceAreaParameters , ServiceAreaSolveResult , ServiceAreaSolveResult , ShadowCastViewModel , SiteLayerInfo , SizeStop , SketchViewModel , SlicePlane , SlicePlane , SliceViewModel , Slide , SliderViewModel , SnappingControlsViewModel , SnappingOptions , SpatialReference , StatisticDefinition , StatisticDefinition , Stop , StylePattern3D , Sublayer , SunnyWeather , SwipeViewModel , SwitchInput , Symbol , Symbol3DLayer , SynthesizeAssociationGeometriesParameters , Task , TemplateItem , TemplateItemGroup , TemplateOptions , Terminal , TerminalConfiguration , TextBoxInput , TileInfo , TileMatrixSet , TimeExtent , TimeInfo , TimeInterval , TimePickerViewModel , TimeSliderViewModel , TopFeaturesQuery , TopFilter , TraceLocation , TraceParameters , TraceResult , TrackViewModel , TravelMode , TrimExtendParameters , TrimExtendParameters , UI , UniqueValueInfo , UpdateWorkflowData , UtilityNetworkTraceViewModel , VectorFieldRenderer , View , ViewAnimation , ViewState , Viewing , Viewpoint , VisualVariable , WMSSublayer , WMTSStyle , WMTSSublayer , WeatherViewModel , Widget , Workflow , ZoomViewModel
起始版本:GeoScene API for JavaScript 4.0

Accessor 是一个抽象类,它便于访问实例属性,也是一种监视属性更改的机制。Accessor 的每个子类都定义了可直接访问或使用 get()set() 方法访问的属性。可以使用 watch() 方法监视属性更改。

另请参阅:

属性概述

名称 类型 描述
String更多信息

类的名称。

更多信息Accessor

属性详情

declaredClass Stringreadonly
起始版本:GeoScene API for JavaScript 4.7

类的名称。声明的类名的格式为 geoscene.folder.className

方法概述

名称 返回类型 描述
Accessor更多信息

创建调用此方法的类的子类。

更多信息Accessor
*更多信息

获取属性的值。

更多信息Accessor
*更多信息

设置属性的值。

更多信息Accessor
WatchHandle更多信息

监视实例上的属性更改。

更多信息Accessor

方法详情

createSubclass(classDefinition){Accessor}static
起始版本:GeoScene API for JavaScript 4.21

创建调用此方法的类的子类。

参数:
classDefinition Object

具有可混入新创建的类的属性和方法的对象。

返回:
类型 说明
Accessor 返回新创建的类的构造函数。
get(path){*}

获取属性的值。

属性的名称可以引用实例中的属性。有关更多信息,请参阅以下 使用属性指南部分。

view.get("scale");

它也可以是实例中更深层次的属性的路径。如果路径中的属性不存在,get() 则返回 undefined

let title = map.get("basemap.title");

// equivalent of
let title = map.basemap && map.basemap.title || undefined;
参数:
path String

要获取的属性的路径。

返回:
类型 说明
* 属性值。
set(path, value){*}

设置属性的值。

使用属性名称和值调用 set() 以更改属性的值。

// setting the basemap of the map
map.set("basemap", "tianditu-vector");
// is equivalent to
map.basemap = "tianditu-vector";

// currying set
let updateViewScale = view.set.bind(view, "scale");
updateViewScale(5000);

set() 可以使用属性和值的路径调用。如果路径中的属性不存在,则不会设置该属性。

// updating the title of the basemap
map.set("basemap.title", "World Topographic Map");

// is equivalent to
if (map.basemap != null) {
  map.basemap.title = "World Topographic Map";
}

可以将具有键值对的对象传递给 set() 以一次更新多个属性。

// setting a viewpoint on the view
view.set({
  center: [-4.4861, 48.3904],
  scale: 5000
});

// currying set
let updateView = view.set.bind(view);

updateView({
  center: [-4.4861, 48.3904],
  scale: 5000
});
参数:

要设置的属性的路径,或键值对的对象。

value *

要在属性上设置的新值。

返回:
类型 说明
* 实例。
watch(path, callback){WatchHandle}

监视实例上的属性更改。

监视属性更改对于跟踪对象的更改至关重要。要开始监视属性的更改,请使用属性名称和回调函数调用 watch(),该回调函数将在每次属性更改时执行。

let handle = mapview.watch("scale", function(newValue, oldValue, propertyName, target) {
  console.log(propertyName + " changed from " + oldValue + " to " + newValue);
});

要停止监视更改,请在 watch() 返回的对象上调用 remove() 方法。

handle.remove();

存储来自 watch() 的结果对象以正确清理引用非常重要。

let viewHandles = [];
function setView(view) {
  // remove the handles for the current view.
  viewHandles.forEach(function(handle) {
    handle.remove();
  });
  viewHandles.length = 0;

  this.view = view;

  // watch for properties on the newly set view.
  if (view) {
    viewHandles.push(
      view.watch("scale", scaleWatcher);
    );
  }
}

setView(mapView);
setView(null);

get()set() 一样,可以通过传递路径来观察对象层次结构深处的属性。如果路径中的属性不存在,则使用 undefined 调用监视回调。

let view = new SceneView({
  map: new Map({
    basemap: "tianditu-vector"
  })
});

view.watch("map.basemap.title", function(newValue, oldValue) {
  console.log("basemap's title changed from " + oldValue + " to " + newValue);
});

view.map.basemap = "tianditu-vector";
// output: "basemap's title changed from Streets to Topographic"

view.map = null;
// output: "basemap's title changed from Topographic to undefined"

传递以逗号分隔的属性路径列表或属性路径数组,以使用相同的回调观察多个属性。使用回调调用的第三个参数来确定更改了哪些属性。

view.watch("center, scale, rotation", function(newValue, oldValue, propertyName) {
  console.log(propertyName + " changed");
});

// equivalent of
view.watch(["center", "scale", "rotation"], function(newValue, oldValue, propertyName) {
  console.log(propertyName + " changed");
});

// equivalent of
let callback = function(newValue, oldValue, propertyName) {
  console.log(propertyName + " changed");
}
view.watch("center", callback);
view.watch("scale", callback);
view.watch("rotation", callback);

Accessor 属性值更改后不会立即为属性调用监视回调。相反,当一个属性的值发生变化并且如果该属性被监视时,Accessor 会安排一个通知,然后在以后处理该通知。可以查看像 view.scale 这样频繁更改的属性,而无需限制回调。

// Divides the view.scale three times
view.watch("scale", function(newValue, oldValue) {
  console.log("view's scale changed from " + oldValue + " to " + newValue);
});
console.log("current view scale: " + view.scale);
view.scale = view.scale / 2;
view.scale = view.scale / 2;
view.scale = view.scale / 2;
console.log("current view scale: " + view.scale);

// output the following:
// current view scale: 36978595.474472
// current view scale: 4622324.434309
// view's scale changed from 36978595.474472 to 4622324.434309

watch() 带有一个实用模块 reactiveUtils,它为查看属性提供了便利的功能。

参数:

要监视的一个或多个属性。可以将多个属性指定为逗号分隔的列表。

callback watchCallback

属性值更改时要执行的回调。

返回:
类型 说明
WatchHandle 监视句柄。
另请参阅:

类型定义

watchCallback(newValue, oldValue, propertyName, target)

当监视的属性更改时调用的回调。

参数:
newValue *

被监视属性的新值。

oldValue *

被监视属性的旧值。

propertyName String

属性名称。

target Accessor

包含被监视属性的对象。

WatchHandle Object

表示可以删除的监视或事件处理程序。

属性:
remove Function

移除监视句柄。

示例:
let handle = reactiveUtils.watch(() => map.basemap, (newVal) => {
  // Each time the value of map.basemap changes, it is logged in the console
  console.log("new basemap: ", newVal);
});

// When remove() is called on the watch handle, the map no longer watches for changes to basemap
handle.remove();

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