Documentation
Reference
2.1.53 (current version)
collection
interactivityModel
Interfaces

IPolygonGeometryAccess

Extends IFreezable.

Interface for accessing the "Polygon" geometry.

Constructor | Fields | Events | Methods

Constructor

IPolygonGeometryAccess()

Fields

NameTypeDescription
eventsIEventManager

Event manager for the object.

Inherited from IFreezable.

Events

NameDescription
change

Changed coordinates. Instance of the Event class. Names of fields that are available via the Event#get method:

  • oldCoordinates - Old coordinates
  • newCoordinates - New coordinates.
  • oldFillRule - Old fill rule.
  • newFillRule - New fill rule.

Methods

NameReturnsDescription
contains(position)

Boolean

Checks whether the passed point is located inside the polygon.

freeze()

IFreezable

Switches the object to "frozen" mode.

Inherited from IFreezable.

get(index)

Number[][]

Returns coordinates of the contour with the specified index.

getChildGeometry(index)

ILinearRingGeometryAccess

Creates and returns an ILinearRingGeometryAccess object for the specified contour.

getClosest(anchorPosition)

Object

Searches for the point nearest to "anchorPosition" on the polygon contour.

getCoordinates()

Number[][][]

Returns an array of geometry coordinates.

getFillRule()

String

Returns ID of the fill rule.

getLength()

Integer

Returns the number of contours in the geometry.

insert(index, path)

IPolygonGeometryAccess

Adds a new contour with the specified index.

isFrozen()

Boolean

Returns true if the object is in "frozen" mode, otherwise false.

Inherited from IFreezable.

remove(index)

ILinearRingGeometryAccess

Removes the contour with the specified index.

set(index, path)

IPolygonGeometryAccess

Sets coordinates of the contour with the specified index.

setCoordinates(coordinates)

IPolygonGeometryAccess

Sets an array of geometry coordinates.

setFillRule(fillRule)

IPolygonGeometryAccess

Sets the polygon's fill rule.

splice(index, number)

ILinearRingGeometryAccess[]

Deletes a defined number of contours, starting from the specified index. New contours can be added in place of the deleted ones. Coordinates of the new contours can be passed as additional arguments after the "number" parameter.

unfreeze()

IFreezable

Switches the object to active mode.

Inherited from IFreezable.

Events details

change

Changed coordinates. Instance of the Event class. Names of fields that are available via the Event#get method:

  • oldCoordinates - Old coordinates
  • newCoordinates - New coordinates.
  • oldFillRule - Old fill rule.
  • newFillRule - New fill rule.

Methods details

contains

{Boolean} contains(position)

Checks whether the passed point is located inside the polygon.

Returns indicator for whether the point belongs to the polygon.

Parameters:

ParameterDefault valueDescription
position*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Number[]

Coordinates of a point.

* Mandatory parameter/option.

Example:

var myPolygon = new ymaps.geometry.Polygon([
        [[69, 45], [68, 55], [86, 56]]
    ]);

// This method only works with a map that is set correctly.
myPolygon.options.setParent(myMap.options);
myPolygon.setMap(myMap);

// Checking whether the click point is inside the polygon with the geometry set above.
myMap.events.add('click', function (e) {
    alert(myPolygon.contains(e.get('coords')) ? 'Hit!' : 'Miss!');
});

get

{Number[][]} get(index)

Returns coordinates of the contour with the specified index.

Parameters:

ParameterDefault valueDescription
index*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

Contour index.

* Mandatory parameter/option.

getChildGeometry

{ILinearRingGeometryAccess} getChildGeometry(index)

Creates and returns an ILinearRingGeometryAccess object for the specified contour.

Returns the geometry object that corresponds to the specified contour.

Parameters:

ParameterDefault valueDescription
index*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

Contour index.

* Mandatory parameter/option.

getClosest

{Object} getClosest(anchorPosition)

Searches for the point nearest to "anchorPosition" on the polygon contour.

Returns an object with the following fields:

  • position - The point on the polygon contour that is nearest to "anchorPosition".
  • distance - Distance from "anchorPosition" to "position".
  • closestPointIndex - Index of the polygon vertex closest to "position".
  • nextPointIndex - Index of the polygon vertex that follows "position".
  • prevPointIndex - Index of the polygon vertex that precedes "position".
  • pathIndex - Index of the polygon contour that the found point is associated with.
The "nextPointIndex" and "prevPointIndex" fields may be omitted if "position" coincides with one of the polygon vertexes.

Parameters:

ParameterDefault valueDescription
anchorPosition*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Number[]

Coordinates of a point for which the nearest vertex on the polygon contour is calculated.

* Mandatory parameter/option.

Example:

var myPolygon = new ymaps.Polygon([
        [[69, 45], [68, 55], [86, 56], [87, 43]],
        [[60, 48], [62, 60], [80, 62], [80, 48]]
    ]),
    myPoint = new ymaps.Placemark([74, 52]),
    // Constructing the shortest normal from the polygon contour to the point:
    normalVec = new ymaps.Polyline([
        myPolygon.geometry.getClosest(myPoint.geometry.getCoordinates()).position,
        myPoint.geometry.getCoordinates()
    ]));

myMap.geoObjects
    .add(myPolygon)
    .add(myPoint),
    .add(normalVec);

getCoordinates

{Number[][][]} getCoordinates()

Returns an array of geometry coordinates.

getFillRule

{String} getFillRule()

Returns ID of the fill rule.

Example:

var myPolygon = new ymaps.Polygon([
        [[69, 45], [68, 55], [86, 56], [87, 43]],
        [[60, 48], [62, 60], [80, 62], [80, 48]]
    ]),
    middlePoint = [74, 52];

myMap.geoObjects.add(myPolygon);

// Checking a point that falls on the intersection of two contours
// by default (with the value fillRule == 'evenOdd') forms an opening (hole).
alert(myPolygon.geometry.contains(middlePoint)); // => false

// After setting the value to 'nonZero', all intersections are also "filled in",
// so now the point is in the polygon.
myPolygon.geometry.setFillRule('nonZero');
alert(myPolygon.geometry.contains(middlePoint)); // => true

getLength

{Integer} getLength()

Returns the number of contours in the geometry.

insert

Adds a new contour with the specified index.

Returns self-reference.

Parameters:

ParameterDefault valueDescription
index*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

Contour index.

path*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Number[][]

Contour coordinates.

* Mandatory parameter/option.

remove

Removes the contour with the specified index.

Returns the deleted contour.

Parameters:

ParameterDefault valueDescription
index*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

Contour index.

* Mandatory parameter/option.

set

Sets coordinates of the contour with the specified index.

Returns self-reference.

Parameters:

ParameterDefault valueDescription
index*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

Contour index.

path*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Number[][]

Contour coordinates.

* Mandatory parameter/option.

setCoordinates

Sets an array of geometry coordinates.

Returns self-reference.

Parameters:

ParameterDefault valueDescription
coordinates*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Number[][][]

Geometry coordinates.

* Mandatory parameter/option.

setFillRule

Sets the polygon's fill rule.

Returns self-reference.

Parameters:

ParameterDefault valueDescription
fillRule*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: String

ID of the fill rule.

* Mandatory parameter/option.

splice

Deletes a defined number of contours, starting from the specified index. New contours can be added in place of the deleted ones. Coordinates of the new contours can be passed as additional arguments after the "number" parameter.

Returns the deleted contours.

Parameters:

ParameterDefault valueDescription
index*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

The index to start from for removing and adding contours.

number*
[no-highlight[

* Mandatory parameter/option.

]no-highlight]

Type: Integer

The number of contours to be deleted.

* Mandatory parameter/option.