# OpenLayers.Control.Snapping

Acts as a snapping agent while editing vector features.

#### Inherits from

- OpenLayers.Control

Summary

OpenLayers.Control.Snapping | Acts as a snapping agent while editing vector features.
Constants |
EVENT_TYPES | {Array(String)} Supported application event types.
DEFAULTS | Default target properties.
Properties |
greedy | {Boolean} Snap to closest feature in first layer with an eligible feature.
precedence | {Array} List representing precedence of different snapping types.
resolution | {Float} The map resolution for the previously considered snap.
geoToleranceCache | {Object} A cache of geo-tolerances.
layer | {OpenLayers.Layer.Vector} The current editable layer.
feature | {OpenLayers.Feature.Vector} The current editable feature.
point | {OpenLayers.Geometry.Point} The currently snapped vertex.
Constructor |
OpenLayers.Control.Snapping | Creates a new snapping control.
Functions |
setLayer | Set the editable layer.
setTargets | Set the targets for the snapping agent.
addTargetLayer | Add a target layer with the default target config.
addTarget | Add a configured target layer.
removeTargetLayer | Remove a target layer.
removeTarget | Remove a target.
activate | Activate the control.
deactivate | Deactivate the control.
onSketchModified | Registered as a listener for the sketchmodified event on the editable layer.
onVertexModified | Registered as a listener for the vertexmodified event on the editable layer.
considerSnapping |
testTarget |
getGeoTolerance | Calculate a tolerance in map units given a tolerance in pixels.
destroy | Clean up the control.

### Constants

### EVENT_TYPES

{Array(String)} Supported application event types. Register a listener for a particular event with the following syntax:

>     control.events.register(type, obj, listener);

Listeners will be called with a reference to an event object. The properties of this event depends on exactly what happened.

#### Supported control event types (in addition to those from OpenLayers.Control)

beforesnap | Triggered before a snap occurs. Listeners receive an event object with point, x, y, distance, layer, and snapType properties. The point property will be original point geometry considered for snapping. The x and y properties represent coordinates the point will receive. The distance is the distance of the snap. The layer is the target layer. The snapType property will be one of “node”, “vertex”, or “edge”. Return false to stop snapping from occurring.
snap | Triggered when a snap occurs. Listeners receive an event with point, snapType, layer, and distance properties. The point will be the location snapped to. The snapType will be one of “node”, “vertex”, or “edge”. The layer will be the target layer. The distance will be the distance of the snap in map units.
unsnap | Triggered when a vertex is unsnapped. Listeners receive an event with a point property.

### DEFAULTS

Default target properties.

### Properties

### greedy

{Boolean} Snap to closest feature in first layer with an eligible feature. Default is true.

### precedence

{Array} List representing precedence of different snapping types. Default is “node”, “vertex”, “edge”.

### resolution

{Float} The map resolution for the previously considered snap.

### geoToleranceCache

{Object} A cache of geo-tolerances. Tolerance values (in map units) are calculated when the map resolution changes.

### layer

{OpenLayers.Layer.Vector} The current editable layer. Set at construction or after construction with setLayer.

### feature

{OpenLayers.Feature.Vector} The current editable feature.

### point

{OpenLayers.Geometry.Point} The currently snapped vertex.

### Constructor

### OpenLayers.Control.Snapping

Creates a new snapping control. A control is constructed with an editable layer and a set of configuration objects for target layers. While the control is active, dragging vertices while drawing new features or modifying existing features on the editable layer will engage snapping to features on the target layers. Whether a vertex snaps to a feature on a target layer depends on the target layer configuration.

#### Parameters

options | {Object} An object containing all configuration properties for the control.

#### Valid options

layer | {OpenLayers.Layer.Vector} The editable layer. Features from this layer that are digitized or modified may have vertices snapped to features from any of the target layers.
targets | {Array(Object | OpenLayers.Layer.Vector)} A list of objects for configuring target layers. See valid properties of the target objects below. If the items in the targets list are vector layers (instead of configuration objects), the defaults from the <defaults> property will apply. The editable layer itself may be a target
layer | allowing newly created or edited features to be snapped to existing features from the same layer. If no targets are provided the layer given in the constructor (as layer) will become the initial target.
defaults | {Object} An object with default properties to be applied to all target objects.
greedy | {Boolean} Snap to closest feature in first target layer that applies. Default is true. If false, all features in all target layers will be checked and the closest feature in all target layers will be chosen. The greedy property determines if the order of the target layers is significant. By default, the order of the target layers is significant where layers earlier in the target layer list have precedence over layers later in the list. Within a single layer, the closest feature is always chosen for snapping. This property only determines whether the search for a closer feature continues after an eligible feature is found in a target layer.

#### Valid target properties

layer | {OpenLayers.Layer.Vector} A target layer. Features from this layer will be eligible to act as snapping target for the editable layer.
tolerance | {Float} The distance (in pixels) at which snapping may occur. Default is 10.
node | {Boolean} Snap to nodes (first or last point in a geometry) in target layer. Default is true.
nodeTolerance | {Float} Optional distance at which snapping may occur for nodes specifically. If none is provided, <tolerance> will be used.
vertex | {Boolean} Snap to vertices in target layer. Default is true.
vertexTolerance | {Float} Optional distance at which snapping may occur for vertices specifically. If none is provided, <tolerance> will be used.
edge | {Boolean} Snap to edges in target layer. Default is true.
edgeTolerance | {Float} Optional distance at which snapping may occur for edges specifically. If none is provided, <tolerance> will be used.
filter | {OpenLayers.Filter} Optional filter to evaluate to determine if feature is eligible for snapping. If filter evaluates to true for a target feature a vertex may be snapped to the feature.
minResolution | {Number} If a minResolution is provided, snapping to this target will only be considered if the map resolution is greater than or equal to this value (the minResolution is inclusive). Default is no minimum resolution limit.
maxResolution | {Number} If a maxResolution is provided, snapping to this target will only be considered if the map resolution is strictly less than this value (the maxResolution is exclusive). Default is no maximum resolution limit.

### Functions

### setLayer

> setLayer: function( | layer | )

Set the editable layer. Call the setLayer method if the editable layer changes and the same control should be used on a new editable layer. If the control is already active, it will be active after the new layer is set.

#### Parameters

layer | {OpenLayers.Layer.Vector} The new editable layer.

### setTargets

> setTargets: function( | targets | )

Set the targets for the snapping agent.

#### Parameters

targets | {Array} An array of target configs or target layers.

### addTargetLayer

> addTargetLayer: function( | layer | )

Add a target layer with the default target config.

#### Parameters

layer | {OpenLayers.Layer.Vector} A target layer.

### addTarget

> addTarget: function( | target | )

Add a configured target layer.

#### Parameters

target | {Object} A target config.

### removeTargetLayer

> removeTargetLayer: function( | layer | )

Remove a target layer.

#### Parameters

layer | {OpenLayers.Layer.Vector} The target layer to remove.

### removeTarget

> removeTarget: function( | target | )

Remove a target.

#### Parameters

target | {Object} A target config.

#### Returns

{Array} The targets array.

### activate

> activate: function()

Activate the control. Activating the control registers listeners for editing related events so that during feature creation and modification, moving vertices will trigger snapping.

### deactivate

> deactivate: function()

Deactivate the control. Deactivating the control unregisters listeners so feature editing may proceed without engaging the snapping agent.

### onSketchModified

> onSketchModified: function( | event | )

Registered as a listener for the sketchmodified event on the editable layer.

#### Parameters

event | {Object} The sketch modified event.

### onVertexModified

> onVertexModified: function( | event | )

Registered as a listener for the vertexmodified event on the editable layer.

#### Parameters

event | {Object} The vertex modified event.

### considerSnapping

> considerSnapping: function( | point,
> | loc | )

#### Parameters

point | {OpenLayers.Geometry.Point} The vertex to be snapped (or unsnapped).
loc | {OpenLayers.Geometry.Point} The location of the mouse in map coords.

### testTarget

> testTarget: function( | target,
> | loc | )

#### Parameters

target | {Object} Object with target layer configuration.
loc | {OpenLayers.Geometry.Point} The location of the mouse in map coords.

#### Returns

{Object} A result object with rank, dist, x, and y properties. Returns null if candidate is not eligible for snapping.

### getGeoTolerance

> getGeoTolerance: function( | tolerance,
> | resolution | )

Calculate a tolerance in map units given a tolerance in pixels. This takes advantage of the geoToleranceCache when the map resolution has not changed.

#### Parameters

tolerance | {Number} A tolerance value in pixels.
resolution | {Number} Map resolution.

#### Returns

{Number} A tolerance value in map units.

### destroy

> destroy: function()

Clean up the control.

Instances of OpenLayers.Layer.Vector are used to render vector data from a variety of sources.
Vector features use the OpenLayers.Geometry classes as geometry description.
Point geometry class.

> setLayer: function( | layer | )

Set the editable layer.

> setTargets: function( | targets | )

Set the targets for the snapping agent.

> addTargetLayer: function( | layer | )

Add a target layer with the default target config.

> addTarget: function( | target | )

Add a configured target layer.

> removeTargetLayer: function( | layer | )

Remove a target layer.

> removeTarget: function( | target | )

Remove a target.

> activate: function()

Activate the control.

> deactivate: function()

Deactivate the control.

> onSketchModified: function( | event | )

Registered as a listener for the sketchmodified event on the editable layer.

> onVertexModified: function( | event | )

Registered as a listener for the vertexmodified event on the editable layer.

> considerSnapping: function( | point,
> | loc | )

> testTarget: function( | target,
> | loc | )

> getGeoTolerance: function( | tolerance,
> | resolution | )

Calculate a tolerance in map units given a tolerance in pixels.

> destroy: function()

Clean up the control.
Controls affect the display or behavior of the map.
{OpenLayers.Layer.Vector} The current editable layer.
{Object} A cache of geo-tolerances.
Close